我如何在两个命令之间暂停

时间:2018-08-10 19:14:36

标签: c# process command

我想在两个停止和启动服务的dos命令之间暂停。

这是代码: (代码是从How can a windows service programmatically restart itself?借来的)

Process proc = new Process();
ProcessStartInfo psi = New ProcessStartInfo();

psi.CreateNoWindow = true;
psi.FileName = "cmd.exe";
psi.Arguments = "/C net stop YOURSERVICENAMEHERE && net start YOURSERVICENAMEHERE";
psi.LoadUserProfile = false;
psi.UseShellExecute = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo = psi;
proc.Start();

基本上,我想在停止和开始之间放置一个“暂停”:

psi.Arguments = "/C net stop YOURSERVICENAMEHERE && timeout /t 10 /nobreak && net start YOURSERVICENAMEHERE"

1 个答案:

答案 0 :(得分:1)

有几种方法可以在.NET中使用ServiceController类并避免与外壳进行任何交互。

您可以在这里找到它们:MSDN ServiceController

如果您希望通过CMD调用一个进程,则可以创建两个单独的进程,并调用Thread.Sleep(毫秒)或Task.Delay(毫秒)进行等待。此外,请确保在启动过程之后,调用.WaitForExit()方法,以便在继续进行之前完成该过程。

我个人建议使用ServiceController,因为它使用所有.NET组件,而不是依赖于外壳程序。