Multithreading with HttpWebRequest

F

Fieldadvice

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;

}

}

}
 
M

MSFT

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.)
 
F

Fieldadvice

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
 
M

MSFT

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top