如何使用c#重新启动指定的进程 [英] How to restart the specified process using c#

查看:147
本文介绍了如何使用c#重新启动指定的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果指定的进程在一段时间内没有发生变化,例如2分钟,我认为该进程已经死了,应该重启。我应该如何使用C#?谢谢

When the specified process doesn't has changed over a period of time,such as 2 minutes,I think the process is dead, and should be restart. How I should do using C#? Thanks

推荐答案

正如您可能已经注意到的那样,Windows正在监视进程,它正在使用无响应标记标记死亡或死亡的进程。该框架也为您提供了跟踪此标志的方法: http:// msdn .microsoft.com / zh-cn / library / system.diagnostics.process.responding.aspx [ ^ ]。但是,这仅适用于具有UI的进程。服务和其他后台流程可以通过他们自己提到的活动通过相同的工具进行监控。但是如果你做了那个过程,你可以选择比这更好的方式来了解它的状态。
As you have probably noticed, Windows is monitoring processes and it is tagging the dead or dead-alike ones with "not responding" flag. The framework is giving you the means to track this flag too: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.responding.aspx[^]. Still, this is usable only for processes having UI. Services and other background processes can be monitored via the same tools by their activity as you mentioned yourself. But if you made that process you can choose better ways than that to be kept informed about it's status.


无论你在这种情况下做了什么,只需杀死并重新启动一个进程就行了这样的虐待!忘记它,找到问题的根源,修复应用程序或构建一个新的应用程序。不要做坏事。



-SA
No matter what exactly you do in such situation, just killing and restarting a process would be such an abuse! Forget it, get to the root of the problem, fix the application or build a new one. Don't do evil.

—SA






你可以尝试如下。

Hi,

you can try like below.
//check your conditions here

//then check process is running or not
Process[] pname = Process.GetProcessesByName("ProcessName");
if (pname.Length == 0)
  Process.Start("ProcessName");
else
{
    pname[0].CloseMainWindow();
    // Free resources associated with process.
    pname[0].Close();

    //restart the process.
    Process.Start("ProcessName");

    // Wait 2 seconds.
    Thread.Sleep(2000);
}





希望有帮助



hope it helps


这篇关于如何使用c#重新启动指定的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆