-4

I would like to pause in between two dos commands that stop and start a service.

Here is the code: ( code was borrowed from 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();

basically, I want to put a 'pause' in between the stop and start:

psi.Arguments = "/C net stop YOURSERVICENAMEHERE && timeout /t 10 /nobreak && net start YOURSERVICENAMEHERE"
MrLister
  • 634
  • 7
  • 32
  • not any more... – MrLister Aug 10 '18 at 19:19
  • Why don't you simply stop the service, use the many sleeping methods available in NET and then start the second service? Is it mandatory to do on a single command line? – Steve Aug 10 '18 at 19:22
  • it's the same service that is stopped and started... and once the service is stopped... it can't be started again without the second "start" command... (this is INTERNAL to the service ) – MrLister Aug 10 '18 at 19:24

1 Answers1

1

There are ways to do this in .NET using the ServiceController class and avoiding any interaction with the shell.

You can find those here:MSDN ServiceController

If you'd prefer to invoke a process through the CMD, you can create two separate processes and call either Thread.Sleep(milliseconds) or Task.Delay(milliseconds) to wait. Additionally, make sure that after you start your process, call the .WaitForExit() method so that the process completes before moving on.

Personally, I recommend using ServiceController as it uses all .NET components rather than relying on the shell.

trademarq
  • 90
  • 8