【发布时间】:2012-10-03 14:55:25
【问题描述】:
当我启动一个进程时,我无法控制我的文本框。
我的问题是:如何在启动进程时修改文本框?
即使我在进程启动之前尝试修改文本框,它仍然不起作用。
我在这个论坛上找到了一些答案,但解释对我来说有点难以解决我的问题。
我创建了一个小程序,它将运行一个批处理文件来进行备份。备份运行时,我无法修改文本框、禁用按钮等。
我已经看到这是正常的,但我不知道如何实施解决方案。我的最后一次尝试是使用Dispatcher.invoke,如下所示。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
tb_Status.Text = "Ready";
}
public void status()
{
Dispatcher.Invoke(DispatcherPriority.Send, new Action( () => { tb_Status.Text = "The backup is running!"; } ) );
}
public void process()
{
try
{
Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Robocopy.bat";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
tb_Output.Text = File.ReadAllText("Backup\\log.txt");
}
catch (Exception ex)
{
tb_Status.Text = ex.Message.ToString();
}
}
private void Bt_Start_Click(object sender, EventArgs e)
{
status();
Directory.CreateDirectory("Backup");
process();
tb_Status.Text = "The backup finished";
File.Delete("Backup\\log.txt");
}
}
【问题讨论】:
-
1.标签不属于标题。 2. 你没有提出问题,也没有以任何方式描述问题。 3.如果遇到跨线程问题,请阅读this。
-
H.B.,我尝试了您的调度程序测试代码,但在执行 BAT 文件时,此代码再次无法运行。
-
@Bo0m3r:如果它根本没有运行,问题就出在进程上,你也不应该真正使用调度程序。 (也用
@Name,否则没有通知)
标签: c# wpf multithreading textbox