.NET:将用户定义的标记提交到 WebClient 的 UploadStringCompletedEventHandler 的最佳方式是什么
.NET: What is the best way to submit a user-defined tag to WebClient's UploadStringCompletedEventHandler
...如果我想重用处理程序,那么多个同时的服务器响应可以被上下文化?
var fileDownload1 = new WebClient();
string myPostData1 = ...;
string tag1 = "download1"; // <-- where do I put it?
fileDownload1.UploadStringCompleted += new UploadStringCompletedEventHandler(OnFileDonwloadRequestCompleted);
fileDownload1.UploadStringAsync(new Uri("http://www.somewhereinthe.net"), httpverb, myPostData1);
....
var fileDownload2 = new WebClient();
string myPostData2 = ...;
string tag2 = "download2"; // <-- where do I put it?
fileDownload2.UploadStringCompleted += new UploadStringCompletedEventHandler(OnFileDonwloadRequestCompleted);
fileDownload2.UploadStringAsync(new Uri("http://www.somewhereinthe.net"), httpverb, myPostData2);
....
void OnFileDonwloadRequestCompleted(object sender, UploadStringCompletedEventArgs e)
{
string tag = ???? ; // <-- where can I get it?
switch (tag)
{
case "download1":
...
break;
case "download2":
...
break;
}
}
以下是确定哪个上传调用触发了回调函数的方法:
在 UploadStringAsync
方法调用中使用带有第四个参数 object userToken
的重载:
fileDownload1.UploadStringAsync(new Uri("http://www.somewhereinthe.net"),
httpverb, myPostData1, "download1");
并在 OnFileDonwloadRequestCompleted
方法中使用 UserState 属性 取回该值:
string tag = (string)e.UserState;
...如果我想重用处理程序,那么多个同时的服务器响应可以被上下文化?
var fileDownload1 = new WebClient();
string myPostData1 = ...;
string tag1 = "download1"; // <-- where do I put it?
fileDownload1.UploadStringCompleted += new UploadStringCompletedEventHandler(OnFileDonwloadRequestCompleted);
fileDownload1.UploadStringAsync(new Uri("http://www.somewhereinthe.net"), httpverb, myPostData1);
....
var fileDownload2 = new WebClient();
string myPostData2 = ...;
string tag2 = "download2"; // <-- where do I put it?
fileDownload2.UploadStringCompleted += new UploadStringCompletedEventHandler(OnFileDonwloadRequestCompleted);
fileDownload2.UploadStringAsync(new Uri("http://www.somewhereinthe.net"), httpverb, myPostData2);
....
void OnFileDonwloadRequestCompleted(object sender, UploadStringCompletedEventArgs e)
{
string tag = ???? ; // <-- where can I get it?
switch (tag)
{
case "download1":
...
break;
case "download2":
...
break;
}
}
以下是确定哪个上传调用触发了回调函数的方法:
在 UploadStringAsync
方法调用中使用带有第四个参数 object userToken
的重载:
fileDownload1.UploadStringAsync(new Uri("http://www.somewhereinthe.net"),
httpverb, myPostData1, "download1");
并在 OnFileDonwloadRequestCompleted
方法中使用 UserState 属性 取回该值:
string tag = (string)e.UserState;