C# 在任务中运行时未到达进程。退出。运行
获取进程时遇到问题。当我使用任务运行进程时,已退出要激发的事件。请在WPF应用程序中运行。 如果由于进程类的限制,无法获取此退出事件,则我希望在任务完成时更新TextBox.Text。 我试着继续,但也没有任何运气C# 在任务中运行时未到达进程。退出。运行,c#,wpf,task,C#,Wpf,Task,获取进程时遇到问题。当我使用任务运行进程时,已退出要激发的事件。请在WPF应用程序中运行。 如果由于进程类的限制,无法获取此退出事件,则我希望在任务完成时更新TextBox.Text。 我试着继续,但也没有任何运气 async private void Select_Click(object sender, RoutedEventArgs e) { CancellationToken ct = new CancellationToken(); Cancel
async private void Select_Click(object sender, RoutedEventArgs e)
{
CancellationToken ct = new CancellationToken();
CancellationTokenSource cts = new CancellationTokenSource();
FolderBrowserDialog diag;
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
Notification.Text = string.Empty;
diag = new FolderBrowserDialog();
var result = diag.ShowDialog();
var istest = TestMode.IsChecked == true;
if (result == System.Windows.Forms.DialogResult.OK)
{
var path = diag.SelectedPath;
await Task.Run(() =>
{
var processStartInfo = new ProcessStartInfo()
{
FileName = "cmd.exe",
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var command="ping yahoo.com";
var process = new Process() { StartInfo = processStartInfo, EnableRaisingEvents = true };
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.StandardInput.WriteLineAsync(command);
process.ErrorDataReceived += (p, @event) =>
{
Dispatcher.InvokeAsync(() =>
{
Notification.AppendText(String.Format("Error {0} {1}", @event.Data, Environment.NewLine));
}, System.Windows.Threading.DispatcherPriority.Background);
};
process.OutputDataReceived += (p, @event) =>
{
Dispatcher.InvokeAsync(() =>
{
Notification.AppendText(@event.Data + Environment.NewLine);
}, System.Windows.Threading.DispatcherPriority.Background);
};
process.WaitForExit();
process.Exited += (@Sender, args) =>
{
tcs.SetResult(process);
System.Windows.MessageBox.Show("Done");
process.Dispose();
};
});
}
}
async private void Select\u单击(对象发送方,RoutedEventArgs e)
{
CancellationToken ct=新的CancellationToken();
CancellationTokenSource cts=新的CancellationTokenSource();
FolderBrowser对话框诊断;
TaskCompletionSource tcs=新的TaskCompletionSource();
Notification.Text=string.Empty;
diag=新建FolderBrowserDialog();
var result=diag.ShowDialog();
var istest=TestMode.IsChecked==true;
if(result==System.Windows.Forms.DialogResult.OK)
{
var path=diag.SelectedPath;
等待任务。运行(()=>
{
var processStartInfo=新的processStartInfo()
{
FileName=“cmd.exe”,
WindowsStyle=ProcessWindowsStyle.Hidden,
重定向标准输出=真,
RedirectStandardError=true,
重定向标准输入=真,
UseShellExecute=false,
CreateNoWindow=true
};
var command=“ping yahoo.com”;
var process=new process(){StartInfo=processStartInfo,EnableRaisingEvents=true};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.StandardInput.WriteLineAsync(命令);
process.ErrorDataReceived+=(p,@event)=>
{
Dispatcher.InvokeAsync(()=>
{
AppendText(String.Format(“Error{0}{1}”,@event.Data,Environment.NewLine));
},System.Windows.Threading.DispatcherPriority.Background);
};
process.OutputDataReceived+=(p,@event)=>
{
Dispatcher.InvokeAsync(()=>
{
Notification.AppendText(@event.Data+Environment.NewLine);
},System.Windows.Threading.DispatcherPriority.Background);
};
process.WaitForExit();
process.exit+=(@Sender,args)=>
{
设置结果(过程);
System.Windows.MessageBox.Show(“完成”);
process.Dispose();
};
});
}
}
所有其他事件都会在没有任何问题的情况下被触发。
感谢您在订阅
退出
事件之前调用WaitForExit
,因此线程将在那里等待进程退出,并且在进程退出之前从不订阅事件。当WaitForExit
返回进程已经退出时,则只有您订阅exited
事件,该事件将不再触发
因此,在调用WaitForExit
之前,subscribe已退出事件
process.Exited += (@Sender, args) =>
{
tcs.SetResult(process);
System.Windows.MessageBox.Show("Done");
process.Dispose();
};
process.WaitForExit();
实际上我已经试过了,但事件从未触发。@user3373870您确定进程已退出吗?这就是问题所在,进程未退出。我必须使用/C参数强制进程(cmd.exe退出)@user3373870@Sriram Sakthivel,并且我必须将退出的事件修改为process.Exited+=(async(@Sender,args)=>{tcs.TrySetResult(process);wait Dispatcher.InvokeAsync()=>{Notification.AppendText(“Completed”);},System.Windows.Threading.DispatcherPriority.Background);process.Dispose();});