wpf 如何在进程结束之前显示进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24332455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show Progressbar untill a process ends
提问by D.K.
I'm having a problem when I' trying to show a progress bar until a external process ends which I started for the WPF window.
我在尝试显示进度条直到我为 WPF 窗口启动的外部进程结束时遇到问题。
The code is like this:
代码是这样的:
private void Button1_Click(object sender, RoutedEventArgs e)
{
Button1.IsEnabled = false;
Button1.Content = "Please Wait";
ProgressBar1.Visibility = Visibility.Visible;
if (a == 1 && b == 0)
{
var processStartInfo = new ProcessStartInfo(@"External Process Path 1");
processStartInfo.Verb = "runas";
try
{
Process.Start(processStartInfo);
}
catch (Win32Exception ex)
{
MessageBox.Show(ex.ToString(), "Run As",
MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
if (b == 1 && a == 0)
{
var processStartInfo = new ProcessStartInfo(@"External Process Patch 2");
processStartInfo.Verb = "runas";
try
{
Process.Start(processStartInfo);
}
catch (Win32Exception ex)
{
MessageBox.Show(ex.ToString(), "Run As",
MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
Button2.IsEnabled = true;
ProgressBar1.Visibility = Visibility.Hidden; //This is what I want to toggle after process ends
}
I've tried Thread.Sleep(time) method and also for-loop but nothing seems to work. I'm very new in WPF. So, please try to be a little brief.
我试过 Thread.Sleep(time) 方法和 for-loop 但似乎没有任何效果。我对 WPF 很陌生。所以,请尽量简短一些。
Thanks, D.K.
谢谢,DK
回答by flo_badea
do you know how long the external process lasts? if you don't you can try to set the IsIndeterminate property to true on your progress bar. this will show a continuous animation. When your process returns you can set it to false again in order to stop the animation.
你知道外部过程持续多长时间吗?如果不这样做,您可以尝试在进度条上将 IsIndeterminate 属性设置为 true。这将显示一个连续的动画。当您的流程返回时,您可以再次将其设置为 false 以停止动画。
also, in your code, i think you're not waiting for the process to finish. you could do this by using the code below.
另外,在您的代码中,我认为您不是在等待过程完成。你可以使用下面的代码来做到这一点。
Process p = Process.Start("IExplore");
p.WaitForExit();
please note that WaitForExit() blocks the current thread. as a result the app will stop responding. to keep the UI responsive you might like to start your process on a different thread like below.
请注意 WaitForExit() 会阻塞当前线程。因此,应用程序将停止响应。为了保持 UI 响应,您可能希望在不同的线程上启动您的进程,如下所示。
private void onClick_Handler(object sender, EventArgs e) {
//disable button here
Task.Factory.StartNew(() => {
Process p = Process.Start("IExplore");
p.WaitForExit();
//enable button here. make sure to do this on the UI thread
//since you're doing this in the code-behind you should have access
//to the dispatcher
Dispatcher.BeginInvoke((Action)OnUpdateUI);
});
}
private void OnUpdateUI(){
}
回答by yashashwi
In the above code, you are starting a process but not waiting for it to end, so the debugger when executes your:
在上面的代码中,您正在启动一个进程而不是等待它结束,因此调试器在执行您的:
Process.Star("Add Some Task");
, it jumps on to the next statement which is
,它跳到下一个语句
button2.IsEnabled = true;
and so on. As a result your ProgressBar1 is not visible to you.
等等。因此,您看不到 ProgressBar1。
Please wait for the Process to end first. Write
请先等待进程结束。写
Process.WaitForExit();
just after your statement
就在你的陈述之后
Process.Start("Your already started task");
Also you can create an Asynchronous thread to run in parallel.
您也可以创建一个异步线程来并行运行。
Example:
例子:
Task taskA = new Task( () => Console.WriteLine("Hello from taskA."));
taskA.Start();
taskA.Wait();
Also in your above code, you are only displaying ProgressBar but not updating its value with time. So as a result ProgressBar will be visible only with initial value.
同样在上面的代码中,您只显示 ProgressBar 但不随时间更新其值。因此,ProgressBar 将仅在初始值时可见。
For ProgressBar, do something like,
对于 ProgressBar,请执行以下操作,
ProgressBar ProgressBar1 = new ProgressBar();
ProgressBar1.Maximum = 100;
ProgressBar1.Minimum = 0;
Task.Start();
if( Task.Status == "Running")
{
ProgressBar1.Value = 50;
}
if( Task.Status == "completed")
{
ProgressBar1.Value =100;
}
else
{
ProgressBar.Value=0;
Task.Wait();
}
The code above mentioned may not be syntactically right. So do look for correct syntax.
上面提到的代码在语法上可能不正确。所以一定要寻找正确的语法。

