2

我有一个托管在 IIS 上的 ASP.NET Web 表单应用程序。

在应用程序中,我有一个代码片段,它应该从应用程序启动的那一刻起在后台进行一些工作。

问题是当我创建了单例类并从 Global.asax -> Application_Start() 对其进行初始化时,代码仅在第一个用户连接到站点时运行。

如何将其设置为在用户端没有任何依赖的情况下运行?任何想法?

单例类示例:

public sealed class SingeltonBackgroundWork
{
    private static SingeltonBackgroundWork updater = null;
    private static Timer timer;
    private SingeltonBackgroundWork()
    {
        //Milliseconds * Seconds * Minutes
        timer = new Timer(1000 * 60 * 10);
        timer.Elapsed += new ElapsedEventHandler(OnElapsed);
        timer.AutoReset = true;
        timer.Enabled = true;
    }

    private void OnElapsed(object sender, ElapsedEventArgs e)
    {
        //DO SOME WORK
    }

    public static void InitProcess()
    {
        if (updater == null)
        {
            updater = new SingeltonBackgroundWork();
        }
    }
}

Global.asax Application_Start():

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        SingeltonBackgroundWork.InitProcess();
    }
4

0 回答 0