开发者

Monitoring a remote process

开发者 https://www.devze.com 2023-02-03 06:59 出处:网络
I have a method that stops a service(s) but I also need to delete the logs.Usually this is not a problem but the process can take a little bit of time before closing.Again, although the service appear

I have a method that stops a service(s) but I also need to delete the logs. Usually this is not a problem but the process can take a little bit of time before closing. Again, although the service appears stopped, the process does take additional time to close properly. Since the process is still running, I cannot delete the logs so I need to find a way to monitor the .exe to know when its safe to delete the logs.

so far my best option is a 开发者_StackOverflow社区do while loop, unfortunately the first iteration of the delete statement throws an exception and stops the program.

do
{
// delete logs
}
while (System.Diagnostics.Process.GetProcessesByName(processName, machineName).Length > 0);

Im sure there is a simple solution but my lack of experience is the real problem.


This is probably not the best answer either, but you could invert the loop to:

while (System.Diagnostics.Process.GetProcessesByName(processName, machineName).Length > 0)
{
    // delete log files.
}

I would suppose this would evalutate the condition of the loop before executing the contents. But according to your statements, this will not execute the code until the process has exited.

A hackish way around this is to perform a loop, and break out manually once the conditions:

bool CloseProcessOperation = true; // Control variable incase you want to abort the loop
while (CloseProcessOperation)
{
    if (System.Diagnostics.Process.GetProcessesByName(processName, machineName).Length > 0) { break; }  
    // break if no logs exist
    // break for some other condition
    // etc

    // delete logs
}
0

精彩评论

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