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)正是我需要的魔法。非常感谢。