繁体   English   中英

是否应该使用Thread.Sleep()检查进程是否仍在运行?

[英]Should I use Thread.Sleep() to check if a process is still running?

如标题所述,我目前正在制作WPF应用程序,我需要检测应用程序是否正在运行并在关闭时执行某些操作。 我想到的方法是运行一个单独的线程,每两秒钟检查一次进程是否仍在运行,如下所示:

while(Process.GetProcessesByName(processName).Length != 0) {
    Thread.Sleep(2000);
}

//Do something

这将是一个好的解决方案,还有其他方法吗?

谢谢

这将是一个好的解决方案吗?

不,因为这几乎浪费了整个线程。

最好在WPF应用程序中最好使用计时器,最好是DispatcherTimer:

var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };

timer.Tick += (s, e) =>
{
    if (Process.GetProcessesByName(processName).Length > 0)
    {
        // ...
    }
};

timer.Start();

如果要在UI线程上执行冗长的操作,则可以使用等待Task的异步Tick事件处理程序(它将在后台在线程池线程上运行):

var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };

timer.Tick += async (s, e) =>
{
    if (Process.GetProcessesByName(processName).Length > 0)
    {
        await Task.Run(() =>
        {
            // lengthy operation here which runs on a thread pool thread
        });

        // udate UI here
    }
};

timer.Start();

由于您已经在处理流程,因此建议您直接使用它来确定它是否已退出。 您可以为代码使用Exited事件处理程序。 因此,例如:

foreach (var process in Process.GetProcessesByName(processName))
{
  process.Exited += new EventHandler(DoSomething);
}

…

public void DoSomething(object sender, System.EventArgs e)
{
  // do something
}

当具有该名称的进程结束时,这将调用DoSomething

您可以使用每隔x秒执行一次检查的System.Timers.Timer

public sealed partial class Window1 : Window, IDisposable
{
    private readonly System.Timers.Timer _timer = new System.Timers.Timer(TimeSpan.FromSeconds(2).TotalMilliseconds);

    public Window1()
    {
        InitializeComponent();
        _timer.Elapsed += _timer_Elapsed;
        _timer.Start();
    }

    private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        if (Process.GetProcessesByName("processName...").Length == 0)
        {
            _timer.Stop();
            _timer.Dispose();
            //do something...
        }
    }


    public void Dispose()
    {
        _timer.Dispose();
    }
}

DispatcherTimerTick事件不同, TimerElapsed事件始终在线程池线程中排队等待执行。

文档

如果在WPF应用程序中使用System.Timers.Timer ,则值得注意的是System.Timers.Timer在与用户界面(UI)线程不同的线程上运行...使用DispatcherTimer原因,而不是System.Timers.Timer是该DispatcherTimer运行同一个线程中的DispatcherDispatcherPriority可以在设置DispatcherTimer

暂无
暂无

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

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