如何传递参数DownloadProgressChanged或DownloadFileCompleted?



下面是这段代码

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;
}

相关内容

  • 没有找到相关文章

最新更新