在.NET的Azure工作者角色使用的Thread.Sleep或定时器? [英] Using Thread.Sleep or Timer in Azure worker role in .NET?

查看:185
本文介绍了在.NET的Azure工作者角色使用的Thread.Sleep或定时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在Windows服务中,最好是使用定时器,而不是 Thread.sleep代码(超时)。然而,在所有code的例子我能找到那名装卸工人的Azure在互联网上,这是 Thread.sleep代码(超时)这是用来代替定时器

I understand that in a Windows service, it is better to use Timer rather than Thread.Sleep(timeout). However, in all code examples I could find on the Internet that were handling Azure workers, it is Thread.Sleep(timeout) which is used instead of Timer.

即使是默认的$在Visual Studio中的工人的项目模板提供的C $ C使用了 Thread.sleep代码

Even the default code provided in the Worker project template in Visual Studio uses a Thread.Sleep:

public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {
        // This is a sample worker implementation. Replace with your logic.
        Trace.WriteLine("$projectname$ entry point called", "Information");

        while (true)
        {
            Thread.Sleep(10000);
            Trace.WriteLine("Working", "Information");
        }
    }
// ...
}

到目前为止,我一直也是在我的工人,但没有真正理解为什么使用 Thread.sleep代码。所以我的问题是,为什么使用 Thread.sleep代码(超时)中的Azure工作者角色,而不是定时器?导致这种差别的Windows服务和Azure的工作人员之间的差异如何,我们都应该想象这种应用是什么?这是好还是不好用定时器在Azure中的工人?

So far, I've been also using Thread.Sleep in my workers but without really understanding why. So my question is, why using Thread.Sleep(timeout) in Azure worker role rather than Timer? What is the difference between Windows service and Azure worker that leads to this difference in how we are supposed to conceive this kind of application? Is it good or bad to use Timer in Azure workers?

链接到一些资源解释这个基本面的任何解释,是值得欢迎的,因为我无法找到任何东西为止。

Any explanation with links to some resources explaining the fundamentals of this is welcome as I couldn't find anything so far.

推荐答案

的Thread.Sleep()循环的目的是保持运行()办法退出。如果运行()退出,那么你的工人将重新启动。我不知道,你可以有效地实现这一目标用计时器。

The purpose of the Thread.Sleep() loop is to keep the Run() method from exiting. If Run() exits, then your worker will restart. I don't know that you could accomplish that goal effectively with a Timer.

最有可能你的CPU是浪费时间一些微小的量,以什么都不做唤醒线程每1000毫秒。我怀疑这是显著,但窃听我了。我的解决办法是等待一个的CancellationToken代替。

Most likely your CPU is wasting some tiny amount of time to wake up that thread every 1000 msecs in order to do nothing. I doubt it's significant, but it bugged me too. My solution was to wait on a CancellationToken instead.

public class WorkerRole : RoleEntryPoint {
    CancellationTokenSource cancelSource = new CancellationTokenSource();

    public override void Run()
    {
        //do stuff
        cancelSource.Token.WaitHandle.WaitOne();
    }

    public override void OnStop()
    {
        cancelSource.Cancel();
    }
}

这样能使退出而不浪费CPU时间忙等待的run()方法。您也可以使用的CancellationToken其他地方在你的程序来启动您可能需要执行其他的关机操作。

This keeps the Run() method from exiting without wasting CPU time on busy waiting. You can also use the CancellationToken elsewhere in your program to initiate any other shutdown operations you may need to perform.

这篇关于在.NET的Azure工作者角色使用的Thread.Sleep或定时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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