我有一个WPF窗口,该窗口应该更新文本框以告诉用户正在发生什么,然后执行bat文件。 但是,它只是执行这些文件而没有更新文本框。
MainLine.Text = "Running for " + customer;
DataInput.Text = "Running Data input.";
ExecuteProcess(Baseloco + "01_DataInput.bat");
Mailsort.Text = "Running Mailsort.";
ExecuteProcess(Baseloco + "02_Mailsort.bat");
SampleandRecon.Text = "Running sample + recon.";
ExecuteProcess(Baseloco + "03_SampleandRecon.bat");
执行过程只是调用我编写的bat文件的一种方法
所以我想在运行bat文件之前更新文本框并完全完成更新
有帮助吗?
答案 0 :(得分:1)
您应该声明ExecuteProcess方法异步:
private async Task ExecuteProcess(string file)
{
await Task.Run(() =>
{
// run file...
});
}
然后运行您的执行步骤,例如在异步事件处理程序中:
private async void Button_Click(object sender, RoutedEventArgs e)
{
MainLine.Text = "Running for " + customer;
DataInput.Text = "Running Data input.";
await ExecuteProcess(Baseloco + "01_DataInput.bat");
Mailsort.Text = "Running Mailsort.";
await ExecuteProcess(Baseloco + "02_Mailsort.bat");
SampleandRecon.Text = "Running sample + recon.";
await ExecuteProcess(Baseloco + "03_SampleandRecon.bat");
}
答案 1 :(得分:0)
我建议您初始化一个新线程来完成工作,然后使用dispatcher.invoke设置文本框。像这样:
Task.Run(() =>
{
Dispatcher.Invoke(() =>
{
MainLine.Text = "Running for " + customer;
DataInput.Text = "Running Data input.";
});
ExecuteProcess(Baseloco + "01_DataInput.bat");
Dispatcher.Invoke(() => Mailsort.Text = "Running Mailsort.");
ExecuteProcess(Baseloco + "02_Mailsort.bat");
Dispatcher.Invoke(() => SampleandRecon.Text = "Running sample + recon.");
ExecuteProcess(Baseloco + "03_SampleandRecon.bat");
});
编辑:
由于有可能批处理文件可能要并行运行,因此我建议使用以下代码以获得更好的性能:
Task.Run(() =>
{
Dispatcher.Invoke(() =>
{
MainLine.Text = "Running for " + customer;
DataInput.Text = "Running Data input.";
});
ExecuteProcess(Baseloco + "01_DataInput.bat");
});
Task.Run(() =>
{
Dispatcher.Invoke(() => Mailsort.Text = "Running Mailsort.");
ExecuteProcess(Baseloco + "02_Mailsort.bat");
});
Task.Run(() =>
{
Dispatcher.Invoke(() => SampleandRecon.Text = "Running sample + recon.");
ExecuteProcess(Baseloco + "03_SampleandRecon.bat");
});