locked
How can I wait main thread till to WebClient OpenReadCompleted event is fired. RRS feed

  • Question

  • Hi,

    I have written a class which used to download  Xap file using WebClient.

    StartDoenLoad()

    {

    WebClient downloader = new WebClient();

    downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(Downloader_OpenReadCompleted);

    downloader.OpenReadAsync(new Uri(this.fileName, UriKind.Relative));

    // here I want to stop my thread  till "Downloader_OpenReadCompleted" method is called.

    // I want to return  this.downloadStream to other component layer.

    private void Downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)

    {

    this.downloadStream = e.Result;

    }

     

    Any body can help me. ?  I want to call one method and return the result instead of raise an event latter.

     

    Thanks

    Bichitra

     

     

    Wednesday, September 24, 2008 5:17 PM

Answers

  • The problem you are running into is that the networks calls are queued on the ui thread behind StartDownload, so regardless of where you wait, it's just stuck there until the UI thread completes its current request(which you've blocked.) What you need to do is break the operation into two, i.e. start it, let the ui keep on chugging, and then in a separate method handle the completion.

    Wednesday, September 24, 2008 8:20 PM

All replies

  • Here's one idea.

    Create a global flag:

    bool done = false;

    Start a while loop:

    while ( !done )
    {
       Sleep ( some time );
      
       If ( someone wants me to quit )
          done = true;
    }

     

    private void Downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
       this.downloadStream = e.Result;

       done = true;
    }


     

    Wednesday, September 24, 2008 5:47 PM
  • If by main thread you mean the UI thread, I can definitely help you. Give up. You can't do sync io on the UI thread no matter what you try. Your call to OpenReadAsync won't even generate a network call until StartDoenLoad returns.

    Wednesday, September 24, 2008 7:11 PM
  • If that is true, and I am not going to spend any time trying it, then will one of these work?

    StartDoenLoad()
    {
       callFuncToStartOpenReadAsync ():

       wait here.
    }

    StartDoenLoad()
    {
       start storyboard to start OpenReadAsync ():

       wait here.
    }

     

    Wednesday, September 24, 2008 7:43 PM
  • And if the story board won't start until you return then call a function to start the story board.

    Wednesday, September 24, 2008 7:51 PM
  • The problem you are running into is that the networks calls are queued on the ui thread behind StartDownload, so regardless of where you wait, it's just stuck there until the UI thread completes its current request(which you've blocked.) What you need to do is break the operation into two, i.e. start it, let the ui keep on chugging, and then in a separate method handle the completion.

    Wednesday, September 24, 2008 8:20 PM
  • Hi,

    The  Downloader_OpenReadCompleted is called as the StartDownloader() executed. when i was  in debug mode. when then cursor left StartDownloader() method then Downloader_OpenReadCompleted is called.

    Thanks

    Bichitra

    Wednesday, September 24, 2008 9:53 PM
  • Hi,

    I have a button in UI. on the click on this button  I am calling  another class in different assembly load assembly . e,g   CreateObject( string key). it should return object. but event is fired after the executed the CreateObject method.

    Thanks

    Bichitra 

    Wednesday, September 24, 2008 9:59 PM