简体   繁体   English

取消通过System.Diagnostics.Process.Start()启动的进程

[英]Canceling a Process started via System.Diagnostics.Process.Start()

I am writing an application where I have a Process running in a BackgroundWorker . 我正在编写一个应用程序,我在BackgroundWorker运行了一个Process。 I would like to support cancellation from the user interface, but I don't see an easy way to do this. 我想支持从用户界面取消,但我没有看到一个简单的方法来做到这一点。

The Process is actually a fairly long running command line exe. Process实际上是一个相当长的命令行exe。 The output is getting redirected asynchronously via the Progress.OutputDataReceived event and is being used to report progress to the GUI. 输出通过Progress.OutputDataReceived事件异步重定向,并用于向GUI报告进度。

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    using (var process = new Process())
    {
        //...

        process.Start()

        //...

        process.WaitForExit();
    }
}

private void CancelWorker()
{
    worker.CancelAsync();
    // where to actually listen for the cancellation???
}

There doesn't appear to be a way to have the process "listen" for any input from the main thread aside from the StandardInput , but that won't work unless the app itself will response to a specific input to abort. 除了StandardInput之外,似乎没有办法让进程“监听”来自主线程的任何输入,但除非应用程序本身将响应特定的输入以中止,否则这将无效。

Is there a way to cancel a process based on a cancel request from the main thread? 有没有办法根据主线程的取消请求取消进程?

For the purposes of my of the EXE running in the process, I can just call Process.Close() to exit without any side-effects, but the Process object is only known to the worker_DoWork() method, so I'll need to keep track of the Process instance for cancellation purposes... that's why I'm hoping there might be a better way. 出于我在流程中运行的EXE的目的,我可以调用Process.Close()退出而没有任何副作用,但Process对象只有worker_DoWork()方法已知,所以我需要跟踪Process实例以取消目的......这就是为什么我希望可能有更好的方法。

(if it matters, I am targeting .NET 3.5 for compatibility issues.) (如果重要的话,我的目标是.NET 3.5以解决兼容性问题。)

For cooperative cancellation, you need to listen to BackgroundWorker.CancellationPending in BackgroundWorker.DoWork event. 对于合作消除,你需要听BackgroundWorker.CancellationPendingBackgroundWorker.DoWork事件。

How to: Use a Background Worker 如何:使用后台工作程序

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    using (var process = new Process())
    {
        //...

        process.Start()

        //...
        while(true)
        { 
            if ((sender as BackgroundWorker).CancellationPending && !process.HasExited)
            {
              process.Kill();
              break;
            }
            Thread.Sleep(100);
         }

        process.WaitForExit();
    }
}

try this, this might be help you, in easy way 试试这个,这可能会以简单的方式帮助你

private System.Threading.TimerCallback worker;
private System.Threading.Timer workertimer ;
private void worker_DoWork()
{
   //You codes
}
private void StartWorker()
{
   worker = new System.Threading.TimerCallback(worker_DoWork);
   workertimer = new System.Threading.Timer(worker, null, 1000, 1000);
}

private void CancelWorker()
{
    worker.Dispose();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM