简体   繁体   English

监控远程进程

[英]Monitoring a remote process

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. 由于该进程仍在运行,因此我无法删除日志,因此我需要找到一种方法来监视.exe以了解何时可以安全删除日志。

so far my best option is a do while loop, unfortunately the first iteration of the delete statement throws an exception and stops the program. 到目前为止,我最好的选择是do while循环,不幸的是delete语句的第一次迭代会引发异常并停止程序。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM