Quartz.Net乔布斯在Azure中WebRole [英] Quartz.Net Jobs in Azure WebRole

查看:251
本文介绍了Quartz.Net乔布斯在Azure中WebRole的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前移植WCF服务项目交给一个Azure角色。直到现在包含服务图书馆还主办了Quartz.Net的JobFactory一些轻量级的后台处理(perdiodically清理陈旧的电子邮件确认标记)。我必须到code移动到一个单独的辅助角色?

I'm currently porting a WCF Service Project over to an Azure Role. Until now the library containing the service also hosted a Quartz.Net JobFactory for some lightweight background processing (perdiodically cleaning up stale email confirmation tokens). Do I have to move that code into a seperate worker role?

推荐答案

没有你没有设置一个单独的工作角色。

No you don't have to setup a separate worker role.

您只需在您的OnStart您的Web角色()方法来启动一个后台线程。给该线程给定的时间跨度后执行的方法的Timer对象。

You simply have to start a background thread in your OnStart() Method of your Web Role. Give that thread a Timer object that executes your method after the given timespan.

由于这一点,你可以避开新的工作角色。

Due to this you can avoid a new worker role.

class MyWorkerThread 
{
    private Timer timer { get; set; }
    public ManualResetEvent WaitHandle { get; private set; }

    private void DoWork(object state)
    {
        // Do something
    }

    public void Start()
    {
        // Execute the timer every 60 minutes
        WaitHandle = new ManualResetEvent(false);
        timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(60));

        // Wait for the end 
        WaitHandle.WaitOne();
    }
}

class WebRole : RoleEntryPoint
{
    private MyWorkerThread workerThread;

    public void OnStart()
    {
        workerThread = new MyWorkerThread();
        Thread thread = new Thread(workerThread.Start);
        thread.Start();
    }

    public void OnEnd()
    {
        // End the thread
        workerThread.WaitHandle.Set();
    }
}

这篇关于Quartz.Net乔布斯在Azure中WebRole的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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