WebClient回调函数传送参数
WebClient回调函数传递参数使用WebClient类下载数据,具体代码如下:void DownloadDataInBackground(String^
WebClient回调函数传递参数
使用WebClient类下载数据,具体代码如下:
void DownloadDataInBackground(String^ address, String^ dataTemp)
{
try
{
WebClient^ client = gcnew WebClient;
client->BaseAddress = address;
client->DownloadDataCompleted += gcnew DownloadDataCompletedEventHandler(DownloadDataCallback);
client->DownloadDataAsync(gcnew Uri(address));
}
catch(...)
{
Console::WriteLine("{0} download failed!", address);
}
}
其中,address是下载地址,dataTemp是我要传递的数据。
问题是,回调函数的格式只能是void DownloadDataCallback(Object^ sender, DownloadDataCompletedEventArgs^ e),那我怎么才能把我的dataTemp参数传递给回调函数呢?
先谢过了! C++.NET WebClient 参数传递
[解决办法]回调函数的意思不是指你去调用,而是由对方调用,还有你要传参数,要传给谁,谁是接受者
[解决办法]因为不是你调用的所以传什么参数不是由你决定,如果你要传参数完全可以用类成员来达到
String^ dataTemp;
void DownloadDataInBackground(String^ address)
{
try
{
WebClient^ client = gcnew WebClient;
client->BaseAddress = address;
client->DownloadDataCompleted += gcnew DownloadDataCompletedEventHandler(DownloadDataCallback);
client->DownloadDataAsync(gcnew Uri(address));
}
catch(...)
{
Console::WriteLine("{0} download failed!", address);
}
}