473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Multithreading with HttpWebRequest

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;

}

}

}
Nov 17 '05 #1
3 1804
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.)

Nov 17 '05 #2
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" <lu******@online.microsoft.com> wrote in message
news:0k**************@cpmsftngxa06.phx.gbl...
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.)

Nov 17 '05 #3
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.)

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jty202 | last post by:
Am I able to use ASP.net to multithread retrieve webpages? Are there examples of using multithread in ASP.net?
4
by: jty202 | last post by:
Am I able to use ASP.net to multithread retrieve webpages? Are there examples of using multithread in ASP.net?
4
by: jty202 | last post by:
Am I able to use ASP.net to multithread retrieve webpages? Are there examples of using multithread in ASP.net?
4
by: jty202 | last post by:
Am I able to use ASP.net to multithread retrieve webpages? Are there examples of using multithread in ASP.net?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.