C# 将参数传递给WebClient.DownloadFileCompleted事件
我正在使用C# 将参数传递给WebClient.DownloadFileCompleted事件,c#,asynchronous,unity3d,unity5,C#,Asynchronous,Unity3d,Unity5,我正在使用WebClient.DownloadFileAsync()方法,想知道如何将参数传递给WebClient.DownloadFileCompleted事件(或任何其他事件),并在调用的方法中使用它 我的代码: public class MyClass { string downloadPath = "some_path"; void DownloadFile() { int fileNameID = 10; WebClient we
WebClient.DownloadFileAsync()
方法,想知道如何将参数传递给WebClient.DownloadFileCompleted
事件(或任何其他事件),并在调用的方法中使用它
我的代码:
public class MyClass
{
string downloadPath = "some_path";
void DownloadFile()
{
int fileNameID = 10;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += DoSomethingOnFinish;
Uri uri = new Uri(downloadPath + "\" + fileNameID );
webClient.DownloadFileAsync(uri,ApplicationSettings.GetBaseFilesPath +"\" + fileNameID);
}
void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e)
{
//How can i use fileNameID's value here?
}
}
如何将参数传递给
dosomethingonfish()
?您可以使用webClient.QueryString.Add(“FileName”,您的filenameid)代码>以添加额外信息
然后在doSomethingFinish
函数中访问它
使用string myFileNameID=((System.Net.WebClient)(sender)).QueryString[“FileName”]
以接收文件名
代码应该是这样的:
string downloadPath = "some_path";
void DownloadFile()
{
int fileNameID = 10;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DoSomethingOnFinish);
webClient.QueryString.Add("fileName", fileNameID.ToString());
Uri uri = new Uri(downloadPath + "\\" + fileNameID);
webClient.DownloadFileAsync(uri,ApplicationSettings.GetBaseFilesPath +"\\" + fileNameID);
}
void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e)
{
//How can i use fileNameID's value here?
string myFileNameID = ((System.Net.WebClient)(sender)).QueryString["fileName"];
}
void DownloadFile(string url)
{
StartCoroutine(downloadFileCOR(url));
}
IEnumerator downloadFileCOR(string url)
{
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("File Downloaded: " + www.downloadHandler.text);
// Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
}
}
即使这样做可行,您也应该使用Unity的UnityWebRequest
类。您可能还没有听说过它,但它应该是这样的:
string downloadPath = "some_path";
void DownloadFile()
{
int fileNameID = 10;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DoSomethingOnFinish);
webClient.QueryString.Add("fileName", fileNameID.ToString());
Uri uri = new Uri(downloadPath + "\\" + fileNameID);
webClient.DownloadFileAsync(uri,ApplicationSettings.GetBaseFilesPath +"\\" + fileNameID);
}
void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e)
{
//How can i use fileNameID's value here?
string myFileNameID = ((System.Net.WebClient)(sender)).QueryString["fileName"];
}
void DownloadFile(string url)
{
StartCoroutine(downloadFileCOR(url));
}
IEnumerator downloadFileCOR(string url)
{
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("File Downloaded: " + www.downloadHandler.text);
// Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
}
}
我现在能想到的唯一方法是,你可以在一个全局私有字段中保存文件名,然后在dosomethingonfish
@ChristophKn中访问它,这是我最初的解决方案,但我想可能还有更优雅的方法:)在处理多次下载时,这会成为快速响应的混乱库,我现在将检查您的解决方案。目标是在单独的线程上实现下载。UnityWebRequest是我可以在主线程之外使用的东西吗?首先,我想让你明白,你问题中的代码没有使用线程。您使用的是Async
,它不同于线程
。虽然,Async
比Thread
更易于使用。对于UnityWebRequest
,您不能在其他线程中使用它。您不能在其他线程中使用Unity的API。尽管如此,我的回答中的两个代码都是等效的,它们都使用Async
。这是来自MSDN的:“文件是使用线程池自动分配的线程资源异步下载的。”这难道不意味着此函数在内部使用工作线程下载文件吗@节目员:谢谢,我会详细了解这个主题。关于我的原始问题-有没有任何方法可以通过发送方对象将引用传递给对象?与您建议的int类型类似?webClient.QueryString.Add(keyName,value)代码>正是我需要的魔法。非常感谢。