Joe*_*edy 5 c# multithreading asynchronous webclient
我在类中创建了一个异步WebClient请求,如下所示:
public class Downstream
{
public bool StartDownstream()
{
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 [...]");
client.Headers.Add("Content-Type","application/x-www-form-urlencoded");
try
{
byte[] postArray = Encoding.UTF8.GetBytes("somevar=foo&someothervar=bar");
Uri uri = new Uri("http://www.examplesite.com/somepage.php");
client.UploadDataCompleted +=
new UploadDataCompletedEventHandler(client_UploadDataCompleted);
client.UploadDataAsync(uri, postArray);
}
catch (WebException e)
{
MessageBox.Show("A regular Web Exception");
}
catch (NotSupportedException ne)
{
MessageBox.Show("A super Web Exception");
}
return true;
}
void client_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
{
MessageBox.Show("The WebClient request completed");
}
}
Run Code Online (Sandbox Code Playgroud)
然后我创建一个新的类实例并在此处运行该方法:
Downstream Downstream1 = new Downstream();
Downstream1.StartDownstream();
Run Code Online (Sandbox Code Playgroud)
当我这样做时,表单运行的线程似乎挂起,直到WebClient获得响应.为什么是这样?我已经使用过该UploadDataAsync方法,所以它不应该是异步的吗?
这是我的调用堆栈:
[External Code]
> Arcturus.exe!Arcturus.Downstream.StartDownstream() Line 36 + 0x18 bytes C#
Arcturus.exe!Arcturus.MainWindow.btnLogin_Click(object sender, System.Windows.RoutedEventArgs e) Line 111 + 0x12 bytes C#
[External Code]
Run Code Online (Sandbox Code Playgroud)
这就是我运行我的应用程序时发生的一切,只是挂在StartDownstream()和client.UploadDataAsync(uri, postArray);方法上.
| 归档时间: |
|
| 查看次数: |
5547 次 |
| 最近记录: |