开发者

Having one process run another process at timed intervals

开发者 https://www.devze.com 2023-04-11 00:24 出处:网络
Stay with me for a minute: Process A is my main worker process.When it runs, it processes information.It can take anywhere from 30 seconds to 20 minutes to complete.There are multiple variations in t

Stay with me for a minute:

Process A is my main worker process. When it runs, it processes information. It can take anywhere from 30 seconds to 20 minutes to complete. There are multiple variations in this process, and it is not entirely stable. If it crashes, it'开发者_JS百科s not that big of a deal, because it can start where it left off next time it runs.

Process B is to be nothing but my starter process. I'd like it to run Process A at given intervals (like once every 5 minutes). If Process A is already running, then Process B should wait until the next interval to try. IE...

if(!ProcessA.isRunning)
    ProcessA.Run();
else
    Wait Until Next Interval to try

Process A is more or less written. I am figuring it will be its own .exe, rather than use multithreading to accomplish this.

My question is: How do I write Process B that runs a seperate .exe, and hooks into it so I can check to see whether or not it is running?


windows task scheduler already does all of this


Use GetProcessByName as so:

// Get all instances of Notepad running on the local
// computer.
Process [] localByName = Process.GetProcessesByName("notepad");

If get anything in localByName then the process is still running.

MSDN Documentation.


Take a look at the Process class.

Using this class you can retrieve all kind of information about processes in the system. And if you start the process yourself, you don't have to scan all processes, so a slow and error-prone call is prevented.

When have a Process object, you can use WaitForExit to wait until it finishes.

What you can do is:

    var startOtherProcess = true;
    while (startOtherProcess) {
       var watchedProcess = Process.Start("MyProgram.Exe");
       watchedProcess.WaitForExit();
       if (testIfProcessingFinished) {
           startOtherProcess = false;
       }

    }


Here's how the following code works: it check if specified process runs, if so it ignores, otherwise it runs what you need. For intervals use System.Timers.Timer

[DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    public void RunProcess()
    {
        bool createdNew = true;
        using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew))
        {
            if (createdNew)
            {
                // Here you should start another process
                // if it's an *.exe, use System.Diagnostics.Process.Start("myExePath.exe");
            }
            else
            {
                Process current = Process.GetCurrentProcess();
                foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                {
                    if (process.Id != current.Id)
                    {
                        SetForegroundWindow(process.MainWindowHandle);
                        break;
                    }
                }
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号