Faking synchronous calls in Silverlight WP7

后端 未结 4 1904
感情败类
感情败类 2020-12-16 04:11

I\'m porting some code from the full .NET framework to the WP7 version and I\'m running into an issue with synchronous vs async calls.

 string response;
 str         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 04:21

    First I suggest trying to get comfortable with async but if you really want/need to convert an asynchronous call to a synchronous you can use ManualResetEvent to achieve the desired result.

    Here is a quick example if usage:

    public ManualResetEvent _event = new ManualResetEvent(false);
    
    ...
    {
        ...
        var wc = new WebClient();
        wc.OpenReadCompleted += new OpenReadCompletedEventHandler(ReadCompleted);
        wc.OpenReadAsync(uri);
    
        // block until async call is complete
        _event.WaitOne();
    }
    
    private static void ReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        ...
        // set event to unblock caller
        _event.Set();
    }
    

    Now you code will block on the line _event.WaitOne(); until _event.Set(); is called.

    Good Luck!

提交回复
热议问题