Thread.interrupt方法停止长时间睡眠关机的应用程序 - 有没有更好的办法 [英] Thread.Interrupt to stop long sleep at app shutdown - Is there a better approach

查看:317
本文介绍了Thread.interrupt方法停止长时间睡眠关机的应用程序 - 有没有更好的办法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它运行的应用程序生命周期小的后台线程 - 然而,当应用程序关闭,该线程要正常退出



的问题是,线程运行在15分钟的间隔一些代码 - 这意味着它睡觉ALOT



现在,为了得到它的睡眠,我在这折腾中断 - 。我的问题是但是,如果有这更好的方法,因为中断产生ThreadInterruptedException



下面是我的代码(有点伪)要点:

 公共类BackgroundUpdater:IDisposable的
{
专有线程MyThread的;
私人const int的intervalTime = 900000; //15分钟
公共无效的Dispose()
{
myThread.Interrupt();
}

公共无效启动()
{
MyThread的=新主题(ThreadedWork);
myThread.IsBackground = TRUE; //为了确保对应用程序等待线程退出
myThread.Priority = ThreadPriority.BelowNormal;
myThread.Start();
}

私人无效ThreadedWork()
{

{
,而(真)
{
Thread.sleep代码(900000); //15分钟
DoWork的();
}
}
赶上(ThreadInterruptedException)
{
}
}
}


解决方案

有绝对更好的方式 - 既可以使用的 Monitor.Wait /的脉冲而不是睡眠/中断,或者使用的 自动 /的 的ManualResetEvent 。 (你可能需要一个的ManualResetEvent 在这种情况下)。



我个人是一个等待/脉冲风扇,可能是由于它是像Java的wait()/通知()机制。不过,肯定有时间在这里重置事件更加有用。



您的代码将是这个样子:

 私人只读对象挂锁=新的对象(); 
私人挥发性布尔停止=假;

公共无效停止()//如果你想
{
停止=真能做出这样处置;
锁(锁)
{
Monitor.Pulse(挂锁);
}
}

私人无效ThreadedWork()
{
,而(停止!)
{
的DoWork();
锁(锁)
{
Monitor.Wait(挂锁,TimeSpan.FromMinutes(15));
}
}
}

有关详细信息,请参阅我线程教程,特别是对的deadlocks,在,页面href=\"http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml\">等待处理。乔阿尔巴哈利也有覆盖相同的主题,并比较它们的教程



我没有详细看,但我也不会感到惊讶,如果并行扩展也有一些功能,使它更容易些。


I'm having a small background thread which runs for the applications lifetime - however when the application is shutdown, the thread should exit gracefully.

The problem is that the thread runs some code at an interval of 15 minutes - which means it sleeps ALOT.

Now in order to get it out of sleep, I toss an interrupt at it - my question is however, if there's a better approach to this, since interrupts generate ThreadInterruptedException.

Here's the gist of my code (somewhat pseudo):

public class BackgroundUpdater : IDisposable
{
    private Thread myThread;
    private const int intervalTime = 900000; // 15 minutes
    public void Dispose()
    {
        myThread.Interrupt();
    }

    public void Start()
    {
        myThread = new Thread(ThreadedWork);
        myThread.IsBackground = true; // To ensure against app waiting for thread to exit
        myThread.Priority = ThreadPriority.BelowNormal;
        myThread.Start();
    }

    private void ThreadedWork()
    {
        try
        {
            while (true)
            {
                Thread.Sleep(900000); // 15 minutes
                DoWork();
            }
        }
        catch (ThreadInterruptedException)
        {
        }
    }
}

解决方案

There's absolutely a better way - either use Monitor.Wait/Pulse instead of Sleep/Interrupt, or use an Auto/ManualResetEvent. (You'd probably want a ManualResetEvent in this case.)

Personally I'm a Wait/Pulse fan, probably due to it being like Java's wait()/notify() mechanism. However, there are definitely times where reset events are more useful.

Your code would look something like this:

private readonly object padlock = new object();
private volatile boolean stopping = false;

public void Stop() // Could make this Dispose if you want
{
    stopping = true;
    lock (padlock)
    {
        Monitor.Pulse(padlock);
    }
}

private void ThreadedWork()
{
    while (!stopping)
    {
        DoWork();
        lock (padlock)
        {
            Monitor.Wait(padlock, TimeSpan.FromMinutes(15));
        }
    }
}

For more details, see my threading tutorial, in particular the pages on deadlocks, waiting and pulsing, the page on wait handles. Joe Albahari also has a tutorial which covers the same topics and compares them.

I haven't looked in detail yet, but I wouldn't be surprised if Parallel Extensions also had some functionality to make this easier.

这篇关于Thread.interrupt方法停止长时间睡眠关机的应用程序 - 有没有更好的办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆