我似乎陷入了Catch 22.我正在使用BeginInvoke更新我的xaml表单上的控件。但是,beginInvoke需要静态方法,静态方法不能更新xaml控件。
public delegate void UpdateTextCallback(string message);
这是我的BeginInvoke:
public static void runProcess2()
{
Process process = new Process();
process.StartInfo.FileName = @"\\lcsrel\prerelease\lcs\6.0\se_w16_ship\Latest\en-US\amd64fre\Test\Tools\RunUCTest.cmd";
process.StartInfo.Arguments = @"/RunTestsVM /tests:+suite=null /Topology:eepool_ap_onprem /LeaveVmsRunning /VMCopyBuild:Minimal ";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += (sender, e) =>
{
var localSender = sender;
// Prepend line numbers to each line of the output.
if (!String.IsNullOrEmpty(e.Data))
{
try
{
Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new UpdateTextCallback(UpdateText), "\n" + e.Data);
}
catch (Exception ex)
{
string msg = ex.Message;
}
};
};
process.StartInfo.RedirectStandardInput = true;
process.Start();
// Asynchronously read the standard output of the spawned process.
// This raises OutputDataReceived events for each line of output.
process.BeginOutputReadLine();
Task.Factory.StartNew(
() =>
{
process.WaitForExit();
process.Close();
});
}
以下是更新方法:
private static void UpdateText(string message)
{
rt_output.AppendText(message);
}
但是我得到了:
非静态字段,方法或属性Windows4.rt_output需要对象引用。
rt_output
是控件。
如果从此方法中删除static关键字,则在BeginInvoke中出现错误:
非静态字段,方法或属性Windows4.UpdateText(string)需要对象引用。