进程运行c#WPF时无法显示文本
我有一个应用程序,它使用ST-LINK_CLI.exe将固件编程到ST-LINK 用户选择一个固件,按下start,进程开始。然而,这需要相当长的时间为董事会的程序和用户可能认为,该程序已经崩溃。我想要一个文本块来显示“Board Programming…”以便他们知道它正在工作 但是,目前代码在编程之前不会显示文本,我不确定原因。以下是我在单击开始按钮事件中的代码:进程运行c#WPF时无法显示文本,c#,wpf,process,C#,Wpf,Process,我有一个应用程序,它使用ST-LINK_CLI.exe将固件编程到ST-LINK 用户选择一个固件,按下start,进程开始。然而,这需要相当长的时间为董事会的程序和用户可能认为,该程序已经崩溃。我想要一个文本块来显示“Board Programming…”以便他们知道它正在工作 但是,目前代码在编程之前不会显示文本,我不确定原因。以下是我在单击开始按钮事件中的代码: ProcessStartInfo start = new ProcessStartInfo(); //new process
ProcessStartInfo start = new ProcessStartInfo(); //new process start info
start.FileName = STPath; //set file name
start.Arguments = "-C -ME -p " + firmwareLocation + " -v -Run"; //set arguments
start.UseShellExecute = false; //set shell execute (need this to redirect output)
start.RedirectStandardOutput = true; //redirect output
start.RedirectStandardInput = true; //redirect input
start.WindowStyle = ProcessWindowStyle.Hidden; //hide window
start.CreateNoWindow = true; //create no window
using (Process process = Process.Start(start)) //create process
{
try
{
while (process.HasExited == false) //while open
{
process.StandardInput.WriteLine(); //send enter key
programmingTextBlock.Text = "Board Programming...";
}
using (StreamReader reader = process.StandardOutput) //create stream reader
{
result = reader.ReadToEnd(); //read till end of process
File.WriteAllText("File.txt", result); //write to file
}
}
catch { } //so doesn't blow up
finally
{
int code = process.ExitCode; //get exit code
codee = code.ToString(); //set code to string
File.WriteAllText("Code.txt", codee); //save code
}
在进程开始运行之前或进程正在运行时,是否仍然需要显示文本
谢谢
Lucy问题是,当
while
循环运行时,就像在主线程中一样,UI不会刷新。解决这个问题的正确方法是使用或将“有问题”的代码放在另一个线程中
或者,您可以将此programmingTextBlock.Text=“Board Programming…”;
置于while
循环之外,然后添加此行:
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
new Action(delegate { }));
这应该在进入循环之前“刷新”ui。问题是,当
while
循环正在运行时,就像在主线程中一样,ui不会刷新。解决这一问题的正确方法是使用或将“有问题”的代码放在另一个线程中
或者,您可以将此programmingTextBlock.Text=“Board Programming…”;
置于while
循环之外,然后添加此行:
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
new Action(delegate { }));
这应该在进入循环之前“刷新”用户界面。您所说的是等待栏,它应该在长时间执行过程中出现,并在完成时消失。不是吗? 要做到这一点,您应该实现
async/await
模式以防止UI线程阻塞。
在视图模型中:
this.IsBusy = true;
await MyTaskMethodAsync();
this.IsBusy = false;
而MyTaskMethodAsync
返回Task
。
在XAML
中定义Busy条
并绑定到IsBusy
属性,您可以在C#
代码中看到:
<Border Visibility="{Binding IsBusy,Converter={converters:BooleanToSomethingConverter TrueValue='Visible', FalseValue='Collapsed'}}"
Background="#50000000"
Grid.Row="1">
<TextBlock Foreground="White"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Text="Loading. . ."
FontSize="16" />
</Border>
您所说的是等待条,它应该在长时间执行过程中出现,并在完成时消失。不是吗? 要做到这一点,您应该实现
async/await
模式以防止UI线程阻塞。
在视图模型中:
this.IsBusy = true;
await MyTaskMethodAsync();
this.IsBusy = false;
而MyTaskMethodAsync
返回Task
。
在XAML
中定义Busy条
并绑定到IsBusy
属性,您可以在C#
代码中看到:
<Border Visibility="{Binding IsBusy,Converter={converters:BooleanToSomethingConverter TrueValue='Visible', FalseValue='Collapsed'}}"
Background="#50000000"
Grid.Row="1">
<TextBlock Foreground="White"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Text="Loading. . ."
FontSize="16" />
</Border>
要在需要使用另一个thred时操作UI并执行其他操作,我将如何执行此操作?要在需要使用另一个thred时操作UI并执行其他操作,我将如何执行此操作?