Guest User

Untitled

a guest
Aug 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. Having one process run another process at timed intervals
  2. if(!ProcessA.isRunning)
  3. ProcessA.Run();
  4. else
  5. Wait Until Next Interval to try
  6.  
  7. // Get all instances of Notepad running on the local
  8. // computer.
  9. Process [] localByName = Process.GetProcessesByName("notepad");
  10.  
  11. var startOtherProcess = true;
  12. while (startOtherProcess) {
  13. var watchedProcess = Process.Start("MyProgram.Exe");
  14. watchedProcess.WaitForExit();
  15. if (testIfProcessingFinished) {
  16. startOtherProcess = false;
  17. }
  18.  
  19. }
  20.  
  21. [DllImport("user32.dll")]
  22. [return: MarshalAs(UnmanagedType.Bool)]
  23. static extern bool SetForegroundWindow(IntPtr hWnd);
  24.  
  25. public void RunProcess()
  26. {
  27. bool createdNew = true;
  28. using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew))
  29. {
  30. if (createdNew)
  31. {
  32. // Here you should start another process
  33. // if it's an *.exe, use System.Diagnostics.Process.Start("myExePath.exe");
  34. }
  35. else
  36. {
  37. Process current = Process.GetCurrentProcess();
  38. foreach (Process process in Process.GetProcessesByName(current.ProcessName))
  39. {
  40. if (process.Id != current.Id)
  41. {
  42. SetForegroundWindow(process.MainWindowHandle);
  43. break;
  44. }
  45. }
  46. }
  47. }
  48. }
Add Comment
Please, Sign In to add comment