Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Multithreading with HttpWebRequest

Reply
Thread Tools

Multithreading with HttpWebRequest

 
 
Fieldadvice
Guest
Posts: n/a
 
      11-06-2003
Hi,

Im starting a new thread in my main aspx page, but when I run it, it always
runs through the main code first THEN the workerthread. Can somebody shed
some light on the problem??

Here is the code:

public class WebForm1 : System.Web.UI.Page

{

private FeedAsync Feed1;

public XmlDocument state = new XmlDocument();

private void Page_Load(object sender, System.EventArgs e)

{

Feed1 = new
FeedAsync("http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/technology/
rss091.xml", state);

Feed1.WorkerThread.Join();

System.DateTime start = DateTime.Now;

while (Feed1.Completed == false && ((TimeSpan)(DateTime.Now -
start)).Seconds < 20)

{}

if (((TimeSpan)(DateTime.Now - start)).Seconds > 20)

{

Response.Write("TimeOut");

}

else

{

Response.Write(Feed1.Result.InnerText); // <-------Feed1.Result is ALWAYS
NULL

}

}



public class FeedAsync

{

public bool Completed = false;

private HttpWebRequest HTTPRequest = null;

public HttpWebResponse HTTPResponse = null;

public XmlDocument Result = null;

private AsyncCallback cb = null;

public Thread WorkerThread;

public FeedAsync(string URL)

{

state = nstate;

cb = new AsyncCallback(BeginGetResponseAsyncCallback);

HTTPRequest = (HttpWebRequest)HttpWebRequest.Create(URL);

WorkerThread = new Thread(new ThreadStart(Execute));

WorkerThread.Start();

}

public void Execute()

{

HTTPRequest.BeginGetResponse(cb, null);

}

private void BeginGetResponseAsyncCallback(IAsyncResult ar) // <------THIS
FUNCTION ONLY SEEMS TO GET CALLED AFTER THE MAIN THREAD HAS COMPLETED

{

if (ar.IsCompleted)

{

HTTPResponse = (HttpWebResponse)HTTPRequest.EndGetResponse(ar);

System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

StreamReader sr = new StreamReader(HTTPResponse.GetResponseStream(),
encode);

Result = new XmlDocument();

Result.Load(sr);

sr.Close();

HTTPResponse.Close();

Completed = true;

}

}

}


 
Reply With Quote
 
 
 
 
MSFT
Guest
Posts: n/a
 
      11-06-2003
Hello,

I think this there are three threads in your program:

1. ASP.NET working thread which will wait for WorkerThread of class
FeedAsync
2. WorkerThread of class FeedAsync which call HttpWebRequest asynchronously.
3. Thread created by "HTTPRequest.BeginGetResponse(cb, null);" which wait
for response from HttpWebRequest object.

The Join method only force 2 will be executed before 1, but this do nothing
with thread 3.


Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

 
Reply With Quote
 
 
 
 
Fieldadvice
Guest
Posts: n/a
 
      11-06-2003
Yeah I get what you mean, but I get exactly the same behaiviour when I
remove the thread.join().

The way I see it, thread1 should kick-off thread2 and then wait for
thread2/thread3 to flag the completed variable.

Interestingly, if I remove the timeout mechanism, the loop runs
indefinately, because I never get the async callback until thread1
completes.

Fletch

"MSFT" <(E-Mail Removed)> wrote in message
news:0ksK$(E-Mail Removed)...
> Hello,
>
> I think this there are three threads in your program:
>
> 1. ASP.NET working thread which will wait for WorkerThread of class
> FeedAsync
> 2. WorkerThread of class FeedAsync which call HttpWebRequest

asynchronously.
> 3. Thread created by "HTTPRequest.BeginGetResponse(cb, null);" which wait
> for response from HttpWebRequest object.
>
> The Join method only force 2 will be executed before 1, but this do

nothing
> with thread 3.
>
>
> Luke
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>



 
Reply With Quote
 
MSFT
Guest
Posts: n/a
 
      11-07-2003
If you remove the Join method, thread 2 (WorkerThread) will be executed at
same time as Thread 1. The result will can't be predicted. I think you may
only use two thread, send the webrequest synchronously in WorkerThread. How
about this soultion?

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multithreading in Application_Error event handler? ram ASP .Net 1 01-14-2004 08:21 PM
Asynchronous requests vs. multithreading BoomWackaBoom ASP .Net 1 11-15-2003 05:42 PM
MultiThreading Muralidharan Ramakrishnan ASP .Net 1 11-10-2003 01:49 PM
Session State does not Work with Multithreading and SQL Server - Bug??? Ilia ASP .Net 6 11-04-2003 03:55 PM
Multithreading Boris Condarco ASP .Net 5 06-30-2003 01:02 PM



Advertisments