473,785 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.P age

{

private FeedAsync Feed1;

public XmlDocument state = new XmlDocument();

private void Page_Load(objec t sender, System.EventArg s e)

{

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

Feed1.WorkerThr ead.Join();

System.DateTime start = DateTime.Now;

while (Feed1.Complete d == false && ((TimeSpan)(Dat eTime.Now -
start)).Seconds < 20)

{}

if (((TimeSpan)(Da teTime.Now - start)).Seconds > 20)

{

Response.Write( "TimeOut");

}

else

{

Response.Write( Feed1.Result.In nerText); // <-------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(strin g URL)

{

state = nstate;

cb = new AsyncCallback(B eginGetResponse AsyncCallback);

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

WorkerThread = new Thread(new ThreadStart(Exe cute));

WorkerThread.St art();

}

public void Execute()

{

HTTPRequest.Beg inGetResponse(c b, null);

}

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

{

if (ar.IsCompleted )

{

HTTPResponse = (HttpWebRespons e)HTTPRequest.E ndGetResponse(a r);

System.Text.Enc oding encode = System.Text.Enc oding.GetEncodi ng("utf-8");

StreamReader sr = new StreamReader(HT TPResponse.GetR esponseStream() ,
encode);

Result = new XmlDocument();

Result.Load(sr) ;

sr.Close();

HTTPResponse.Cl ose();

Completed = true;

}

}

}
Nov 17 '05 #1
3 1816
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.Be ginGetResponse( 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******@onlin e.microsoft.com > wrote in message
news:0k******** ******@cpmsftng xa06.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.Be ginGetResponse( 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
1418
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
985
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
1024
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
2321
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
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.