与进度指示器C#的下载文件,然后安装文件 [英] C# download file with progress indicator and then install file

查看:150
本文介绍了与进度指示器C#的下载文件,然后安装文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个C#Windows应用程序下载文件(等待的下载完成 - 提供一些进展指标),然后执行下载的文件。我想我要么需要:

I'm trying to write a C# Windows application which downloads a file (waits for that download to complete - providing some indicator of progress) and then executes the downloaded file. I think I either need:


  • 有进度指示器和不会使我的应用程序没有响应
  • 系统同步下载
  • 的等待下载完成,然后异步下载尝试执行它。

  • A synchronous download that has a progress indicator AND doesn't make my application unresponsive
  • An asynchronous download that waits for the download to complete and then attempts to execute it.

前者(同步下载)似乎做我想要什么,但我不能找到一种方法来显示进度的方式,它的出现使我的程序无响应(喜欢它的雄) - 这可能会导致用户关闭该应用程序,而不是等待它完成下载。

The former (synchronous download) seems to do what I want but I can't find a way to indicate progress any way and it appears to make my program non-responsive (like it's hung) - which may cause users to close the application instead of waiting for it to finish downloading.

后来的(异步下载)我已经能够使用进度指示器等来完成,但发生的事情是,下载揭开序幕和我的应用程序立即尝试它完成下载,当然这失败前安装它。

The later (asynchronous download) I've been able to accomplish with a progress indicator etc but what happens is that the download kicks off and my application immediately tries to install it before its finished downloading which of course fails.

所以,最新最好的方式来做到这一点?下面是code我presently有与进度条异步下载。

So whats the best way to accomplish this? Below is the code I presently have for the async download with progress bar.

修改2012/4/10

老code是越来越繁琐的变通所以这里是我presently有。它的工作原理不同之处在于它不更新进度条。

The old code was getting to cumbersome to work around so here is what I presently have. It works except that it doesn't update the progress bar.

    private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {

            List<App> FilesToDownload = e.Argument as List<App>;

            foreach (App Program in FilesToDownload) // Loop through List with foreach
            {
                //string Text = ;
                UpdateRichTextBox("Downloading " + Program.Filename + "... Please wait");

                string Source = Program.DownloadLocation + "/" +  Program.Filename;
                string Destination = System.AppDomain.CurrentDomain.BaseDirectory + Program.Filename;

                WebClient webClient = new WebClient();
                webClient.DownloadFile(Source, @Destination);
            }

            UpdateRichTextBox("File download complete");

            foreach (App Program in FilesToDownload) // Loop through List with foreach
            {
                string Filename = Program.Filename;
                string Arguments = Program.InstallParameters;

                if (Filename != "advisorinstaller.exe")
                {
                    Process p = new Process();
                    p.StartInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory + Filename;
                    p.StartInfo.Arguments = Arguments;
                    p.Start();
                    p.WaitForExit();
                }
                else
                {
                    MessageBox.Show("About to install Belarc Advisor. This may take 10-15 minutes to run - do not shutdown this program. Click OK when ready to proceed.");
                }
            }
    }

    private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
    {
        UpdateRichTextBox("Install Complete");

    }

修改2012/4/11

所以,我已经编辑我的BackgroundWorker做的工作如下:

So I have edited my backgroundworker do work as follows:

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {

            List<App> FilesToDownload = e.Argument as List<App>;

            int counter = 0;
            int percent = 0;

            foreach (App Program in FilesToDownload) // Loop through List with foreach
            {
                percent = ((counter / FilesToDownload.Count) * 100);
                backgroundWorker1.ReportProgress(percent, "");

                UpdateRichTextBox("Downloading " + Program.Filename + "... Please wait");

                string Source = Program.DownloadLocation + "/" +  Program.Filename;
                string Destination = System.AppDomain.CurrentDomain.BaseDirectory + Program.Filename;

                WebClient webClient = new WebClient();
                webClient.DownloadFile(Source, @Destination);


                counter++;

            }

            //backgroundWorker1.ReportProgress(100, "Complete!");
    }

如果我去掉最后一行进度条进行到100%。但它的使用没有进展:

If I uncomment the last line the progress bar proceeds to 100%. But its not progressing using:

percent = ((counter / FilesToDownload.Count) * 100);
backgroundWorker1.ReportProgress(percent, "");

任何想法,为什么?

Any ideas why?

推荐答案

您可以使用的 的BackgroundWorker 执行在后台synchroneous下载。它支持进度报告以及。这将避免阻塞你的UI(因为下载是在不同的线程中运行)。

You can use a BackgroundWorker to perform a synchroneous download in the background. It supports progress reporting as well. This will avoid blocking your UI (because the download is running on a different thread).

这篇关于与进度指示器C#的下载文件,然后安装文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆