在 .NET 中的 Azure 辅助角色中使用 Thread.Sleep 或 Timer? [英] Using Thread.Sleep or Timer in Azure worker role in .NET?

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

问题描述

我知道在 Windows 服务中,最好使用 Timer 而不是 Thread.Sleep(timeout).但是,在 Internet 上我可以找到的所有处理 Azure 工作线程的代码示例中,使用的是 Thread.Sleep(timeout) 而不是 Timer.

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 的 Worker 项目模板中提供的默认代码也使用 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 但并没有真正理解为什么.所以我的问题是,为什么在 Azure 辅助角色中使用 Thread.Sleep(timeout) 而不是 Timer?Windows 服务和 Azure 工作程序之间的区别是什么导致了我们应该如何构思这种应用程序的差异?在 Azure 工作线程中使用 Timer 是好是坏?

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() 循环的目的是让 Run() 方法从退出.如果 Run() 退出,那么您的工作器将重新启动.我不知道您是否可以使用计时器有效地实现该目标.

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();
    }
}

这可以防止 Run() 方法退出而不会在忙等待上浪费 CPU 时间.您还可以在程序中的其他地方使用 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 或 Timer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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