5

已编辑:在 Windows Phone 上,我正在调用HttpWebRequest.BeginGetResponse我开始的单独线程。然后我调用 MessageBox.Show()。问题是在我关闭 MessageBox 之前不会调用回调。

void GetResponseCallback(IAsyncResult asynchronousResult) {
    //Not getting called until I dismiss MessageBox
}

void getWeb() {
    Thread.Sleep(1000);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
    request.Method = "GET";
    request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}

new Thread(getWeb).Start(); //Start a new thread
MessageBox.Show();

MessageBox 是否应该阻止后台线程上的回调?

4

3 回答 3

0

if you want to use MessageBox.Show();

then place a this inside dispacher

void GetResponseCallback(IAsyncResult asynchronousResult) 
{
    Dispatcher.BeginInvoke(() =>
    {
        MessageBox.Show("Done");
    });

}

void getWeb() {
    Thread.Sleep(1000);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
    request.Method = "GET";
    request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}

new Thread(getWeb).Start(); //Start a new thread
于 2013-07-18T09:06:09.967 回答
0

“消息框。显示();” 将阻止您的 ui 线程。

但我认为这不是 GetResponseCallback 不运行的原因。

您可以对其进行测试,评论该代码。

于 2012-10-15T08:43:54.230 回答
0

需要添加引用Microsoft.Phone.Reactive

尝试这个:

private void PushMe_Click(object sender, RoutedEventArgs e)
        {
            var scheduler = Scheduler.NewThread;
            scheduler.Schedule(action => GetWeb());         
            MessageBox.Show("This is a test.", "Test caption.", MessageBoxButton.OK);
        }

        private void GetWeb()
        {
            Thread.Sleep(3000);
            var httpWebRequest = (HttpWebRequest) WebRequest.Create("http://www.stackoverflow.com");
            httpWebRequest.Method = "GET";

            httpWebRequest.BeginGetResponse(BeginGetResponseCallback, httpWebRequest);
        }

        private void BeginGetResponseCallback(IAsyncResult ar)
        {

        }
于 2012-10-12T15:44:25.410 回答