监控远程进程

发布于 2024-10-11 22:47:44 字数 382 浏览 1 评论 0原文

我有一种停止服务的方法,但我还需要删除日志。通常这不是问题,但该过程在关闭之前可能需要一些时间。同样,虽然服务看起来已停止,但该过程确实需要额外的时间才能正确关闭。由于该进程仍在运行,我无法删除日志,因此我需要找到一种方法来监视 .exe 以了解何时可以安全地删除日志。

到目前为止,我最好的选择是 do while 循环,不幸的是,删除语句的第一次迭代抛出异常并停止程序。

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

我确信有一个简单的解决方案,但我缺乏经验才是真正的问题。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞎闹 2024-10-18 22:47:44

这可能也不是最好的答案,但您可以将循环反转为:

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

我想这会在执行内容之前评估循环的条件。但根据你的说法,在进程退出之前不会执行代码。

解决这个问题的一种黑客方法是执行循环,并在满足以下条件后手动中断:

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文