Multithreading with HttpWebRequest

Discussion in 'ASP .Net' started by Fieldadvice, Nov 6, 2003.

  1. Fieldadvice

    Fieldadvice Guest

    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;

    }

    }

    }
     
    Fieldadvice, Nov 6, 2003
    #1
    1. Advertising

  2. Fieldadvice

    MSFT Guest

    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.)
     
    MSFT, Nov 6, 2003
    #2
    1. Advertising

  3. Fieldadvice

    Fieldadvice Guest

    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" <> wrote in message
    news:0ksK$...
    > 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.)
    >
     
    Fieldadvice, Nov 6, 2003
    #3
  4. Fieldadvice

    MSFT Guest

    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.)
     
    MSFT, Nov 7, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Boris Condarco

    Multithreading

    Boris Condarco, Jun 28, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    614
    Boris Condarco
    Jun 30, 2003
  2. Ilia
    Replies:
    6
    Views:
    3,023
    John Saunders
    Nov 4, 2003
  3. Muralidharan Ramakrishnan

    MultiThreading

    Muralidharan Ramakrishnan, Nov 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    421
    Kevin Spencer
    Nov 10, 2003
  4. BoomWackaBoom

    Asynchronous requests vs. multithreading

    BoomWackaBoom, Nov 14, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    649
    Alvin Bruney
    Nov 15, 2003
  5. ram
    Replies:
    1
    Views:
    481
    Sami Vaaraniemi
    Jan 14, 2004
Loading...

Share This Page