进度条,不会及时停止

时间:2015-11-30 20:15:14

标签: c# visual-studio progress-bar

我的问题是,如果安装DownloadInternetFile.exe需要很长时间,状态栏将达到100%。

如果我有一个良好的连接它将完美,但如果我有一个错误的连接我得到一个错误说状态栏值不能超过100%。

if (Install_DownloadInternetFile == "Yes")
{
    if (File.Exists("DownloadInternetFile.exe"))
    {
        var DownloadInternetFile = Process.Start("DownloadInternetFile.exe", "/q /norestart");

        while (!DownloadInternetFile.HasExited)
        {
            this.LoadBar.Value += 1;
            await Task.Delay(TimeSpan.FromSeconds(2));
        }
        DownloadInternetFile.WaitForExit();
    }
    else
    {
        TopMost = false;
        MessageBox.Show("DownloadInternetFile.exe not found", "Error");
    }

}
else
{
    MessageBox.Show("Notselect");
}
LoadBar.Value = 8;

// start DownloadInternetFile2 

if (Install_DownloadInternetFile2 == "Yes")
{

    if (File.Exists("DownloadInternetFile2.exe"))
    {
        var Dotnet45 = Process.Start("DownloadInternetFile2.exe", "/q");

        while (!DownloadInternetFile2.HasExited)
        {
            this.LoadBar.Value += 1;
            await Task.Delay(TimeSpan.FromSeconds(2));
        }
        DownloadInternetFile2.WaitForExit();
    }
    else
    {
        TopMost = false;
        MessageBox.Show("DownloadInternetFile2.exe not found", "Error");
    }

}
else
{
    MessageBox.Show("Notselect");
}

LoadBar.Value = 10;

代码已经安装了10个exe文件。 我想我在这部分缺少某些东西

this.LoadBar.Value += 1;

但无法弄清楚

1 个答案:

答案 0 :(得分:1)

默认情况下,“ProgressBar”类的“Value”属性可以在0到100的范围内。除非您计算,否则您的代码本质上不知道已完成了多少工作。但是,在循环中,您不会计算已完成的工作量,而是根据时间调整进度条的值。因此,如果您知道您的任务将花费相同的时间,无论条件如何,这都是可接受的计算。但是,如果您的操作所花费的时间可能会有所不同,则您需要找到一些其他方法来计算反映操作实际进度的进度条的值。例如,如果您的操作是下载,那么您的栏应该反映下载的字节数除以下载的总字节数。