Sometimes there is a need, a process that only starts one, and must start one.
You can create a Windows service that executes at timed intervals. The number of application processes is queried each time it executes, if not, one is started, and if more than one is started, only one is left
Nonsense not much to say, on the code:
1 Public classClass12 {3 Public voidMain ()4 {5 varObj=system.diagnostics.process.getprocessesbyname ("Calc"). ToList ();6 if(obj. Count = =0)7 startprocess ();8 Else if(obj. Count = =1)9Systemlog.writelogline ("only one process starts, no action required");Ten Else One killprocess (); A startprocess (); - } - the Public voidstartprocess () - { -ProcessStartInfo info =NewProcessStartInfo (); -Info. FileName =@"D:\Calc.exe"; +Info. Arguments =""; -Info. WindowStyle =processwindowstyle.minimized; +Process Pro =Process.Start (info); A //Pro. WaitForExit (); at } - Public voidkillprocess () - { -Process current =process.getcurrentprocess (); -Process[] processes = Process.getprocessesbyname ("Calc"); - foreach(Process processinchprocesses) in { - if(Process. ProcessName = ="Calc") to process. Kill (); + } - } the}
C # application process startup and shutdown