تحديد حالة المستخدم عند تنزيل بشكل غير متزامن

StackOverflow https://stackoverflow.com/questions/1216252

  •  06-07-2019
  •  | 
  •  

سؤال

وVS 2008 SP1

وأنا باستخدام clicent على شبكة الإنترنت لتحميل بعض الملفات بشكل غير متزامن.

ولدي 5 لتحميل الملفات.

ولكن، أريد أن مراقبة كل حمل وترغب في تعيين حالة المستخدم كاسم الملف، وذلك في ProgressCompletedEvent يمكنني التحقق من حالة المستخدم لمعرفة الملفات التي أنجزت؟

وهو مقتطف شفرة قصيرة من ما أحاول القيام به.

// This function will be called for each file that is going to be downloaded.
// is there a way I can set the user state so I know that the download 
// has been completed 
// for that file, in the DownloadFileCompleted Event? 
private void DownloadSingleFile()
{
    if (!wb.IsBusy)
    {        
        //  Set user state here       
        wb.DownloadFileAsync(new Uri(downloadUrl), installationPath);
    }
}


void wb_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    Console.WriteLine("File userstate: [ " + e.UserState + " ]");   
}

void wb_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    Console.WriteLine("File userstate: [ " + e.UserState + " ]");   

    double bytesIn = double.Parse(e.BytesReceived.ToString());
    double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
    double percentage = bytesIn / totalBytes * 100;

    progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());

}
هل كانت مفيدة؟

المحلول

ويمكنك تمرير أي كائن باسم الثالثة حجة لدعوة DownloadFileAsync ()، وستحصل عليه يعود إلى userState. في الحالة الخاصة بك، هل يمكن ببساطة تمرير اسم الملف الخاص بك.

نصائح أخرى

وماذا عن شيء مثل هذا:

private void BeginDownload(
    string uriString,
    string localFile,
    Action<string, DownloadProgressChangedEventArgs> onProgress,
    Action<string, AsyncCompletedEventArgs> onComplete)
{
    WebClient webClient = new WebClient();

    webClient.DownloadProgressChanged +=
        (object sender, DownloadProgressChangedEventArgs e) =>
            onProgress(localFile, e);

    webClient.DownloadFileCompleted +=
        (object sender, AsyncCompletedEventArgs e) =>
            onComplete(localFile, e);

    webClient.DownloadFileAsync(new Uri(uriString), localFile);
}

في كود دعوتكم، هل يمكن بعد ذلك بعض التعليمات البرمجية مثل هذا:

Action<string, DownloadProgressChangedEventArgs> onProgress =
    (string localFile, DownloadProgressChangedEventArgs e) =>
    {
        Console.WriteLine("{0}: {1}/{2} bytes received ({3}%)",
            localFile, e.BytesReceived,
            e.TotalBytesToReceive, e.ProgressPercentage);
    };

Action<string, AsyncCompletedEventArgs> onComplete =
    (string localFile, AsyncCompletedEventArgs e) =>
    {
        Console.WriteLine("{0}: {1}", localFile,
            e.Error != null ? e.Error.Message : "Completed");
    };

downloader.BeginDownload(
    @"http://url/to/file",
    @"/local/path/to/file",
    onProgress, onComplete);

إذا كنت لا تمانع في حوالي مما يجعلها قابلة لإعادة الاستخدام جدا، ويمكنك في الواقع مجرد التخلص من مر في وظائف كل ذلك معا وكتابة عبارات امدا مباشرة في التعليمات البرمجية:

private void BeginDownload(string uriString, string localFile)
{
    WebClient webClient = new WebClient();

    webClient.DownloadProgressChanged +=
        (object sender, DownloadProgressChangedEventArgs e) =>
            Console.WriteLine("{0}: {1}/{2} bytes received ({3}%)",
                localFile, e.BytesReceived,
                e.TotalBytesToReceive, e.ProgressPercentage);

    webClient.DownloadFileCompleted +=
        (object sender, AsyncCompletedEventArgs e) =>
            Console.WriteLine("{0}: {1}", localFile,
                e.Error != null ? e.Error.Message : "Completed");

    webClient.DownloadFileAsync(new Uri(uriString), localFile);
}

ودعا مرتين، وهذا سوف تعطيك سوميتينج إخراج مثل هذا

و/ مسار / إلى / FILE1: تلقت 265/265 بايت (100٪)
/ الطريق / / FILE1: اكتمال
/ الطريق / / FILE2: 2134/2134 بايت تلقت (100٪)
/ الطريق / / FILE2: أنجز

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top