如何将参数传递给 DownloadProgressChanged 或 DownloadFileCompleted?
How to pass parameter to DownloadProgressChanged or 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;
}
我下面有这段代码
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;
}