下面是这段代码
public void DownloadFile(string payloadFile)
{
new Thread(() =>
{
using (WebClient wc = new WebClient())
{
// code removed for brevity
// How to I pass payloadFile parameter to two event handler below?
wc.DownloadProgressChanged += HandleDownloadProgress;
wc.DownloadFileCompleted += HandleDownloadComplete;
wc.DownloadFileAsync(uri, _downloadPath + payloadFile);
}
}).Start();
}
public void HandleDownloadComplete(object sender, AsyncCompletedEventArgs args)
{
// I need to get payloadFile parameter here
}
public void HandleDownloadProgress(object sender, DownloadProgressChangedEventArgs args)
{
// I need to get payloadFile parameter here
}
我如何传递payloadFile参数到两个事件处理程序上面?
将payloadFile
作为userToken
参数:
wc.DownloadFileAsync(uri, _downloadPath + payloadFile, payloadFile);
之后你可以从args
得到它:
public void HandleDownloadComplete(object sender, AsyncCompletedEventArgs args)
{
string payloadFile = (string)args.UserState;
}
public void HandleDownloadProgress(object sender, DownloadProgressChangedEventArgs args)
{
string payloadFile = (string)args.UserState;
}