C# 外部进程的进度条
我目前正在编写一个轻量级程序,将许多命令行和其他外部进程整合到一个应用程序中 目前,我面临着使用系统信息流程提取系统信息的挑战 我已经成功地对按钮进行了编码,以调用系统信息进程,并将输出重定向到文本字段 我现在尝试的是在WPF窗口的底部有一个进度条,因为加载系统信息需要一些时间 因为我不知道如何从外部进程中获得准确的持续时间,所以我尝试使用字幕样式 我已经在stackoverflow()和其他站点上进行了以下示例,但无法确定将代码放在何处,以便在systeminfo运行时进度条滚动,并在完成时停止 下面是我的当前代码(不带C# 外部进程的进度条,c#,wpf,visual-studio-2010,C#,Wpf,Visual Studio 2010,我目前正在编写一个轻量级程序,将许多命令行和其他外部进程整合到一个应用程序中 目前,我面临着使用系统信息流程提取系统信息的挑战 我已经成功地对按钮进行了编码,以调用系统信息进程,并将输出重定向到文本字段 我现在尝试的是在WPF窗口的底部有一个进度条,因为加载系统信息需要一些时间 因为我不知道如何从外部进程中获得准确的持续时间,所以我尝试使用字幕样式 我已经在stackoverflow()和其他站点上进行了以下示例,但无法确定将代码放在何处,以便在systeminfo运行时进度条滚动,并在完成时停
progressbar1.Style=ProgressBarStyle.Marquee;
)
任何关于代码放置位置或使用何种语法的建议都将不胜感激。提前谢谢你
private void btnSystemInfo_Click(object sender, RoutedEventArgs e)
{
// Get system info
Process info = new Process();
info.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
info.StartInfo.FileName = "C:\\Windows\\system32\\systeminfo.exe";
info.StartInfo.Arguments = "-S " + context;
info.StartInfo.UseShellExecute = false;
info.StartInfo.CreateNoWindow = true;
info.StartInfo.RedirectStandardOutput = true;
info.Start();
string infoOutput = info.StandardOutput.ReadToEnd();
info.WaitForExit();
// Write to the txtInfo text box
txtInfo.Text = "System Info: " + infoOutput;
txtInfo.Foreground = Brushes.Black;
txtInfo.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
// Switch to the info tab
tabControl.SelectedIndex = 3;
}
您需要做的是移动
BackgroundWorker
线程中收集系统信息的代码,然后在主UI线程中启动一个选框。一旦您从BackgroundWorker
线程收到信号,表明其工作已完成,请停止选取框并在文本框中显示信息
void ButtonClickEvent()
{
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += new DoWorkEventHandler(MethodToGetInfo);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
//show marquee here
bg.RunWorkerAsync();
}
void MethodToGetInfo(Object sender, DoWorkEventArgs args)
{
// find system info here
}
void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs args)
{
//this method will be called once background worker has completed it's task
//hide the marquee
//update the textbox
//NOTE that this is not a UI thread so you will need BeginInvoke to execute something in the UI thread
}
您需要做的是移动
BackgroundWorker
线程中收集系统信息的代码,然后在主UI线程中启动一个选框。一旦您从BackgroundWorker
线程收到信号,表明其工作已完成,请停止选取框并在文本框中显示信息
void ButtonClickEvent()
{
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += new DoWorkEventHandler(MethodToGetInfo);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
//show marquee here
bg.RunWorkerAsync();
}
void MethodToGetInfo(Object sender, DoWorkEventArgs args)
{
// find system info here
}
void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs args)
{
//this method will be called once background worker has completed it's task
//hide the marquee
//update the textbox
//NOTE that this is not a UI thread so you will need BeginInvoke to execute something in the UI thread
}
如果要同时运行多个进程,则应查看任务库。您可以使任务并行或串行运行,具体取决于您的系统资源。您还可以跟踪完成的工作,以便显示已完成工作的百分比。如果您希望同时运行多个进程,则应查看任务库。您可以使任务并行或串行运行,具体取决于您的系统资源。您还可以跟踪完成的工作,以便显示完成工作的百分比。谢谢Haris Hasan!这就是我一直在寻找的!谢谢你,哈里斯·哈桑!这就是我一直在寻找的!