Search code examples
asp.netpublishglobal-asaxweb-deployment

Thread in Global.asax does not work after publishing the web site


I am developing a web application where I have implemented job scheduler using Global.asax. I have used thread to start the job for particular duration.

It works fine if I run the application through visual studio. But when I publish and deploy it to the server the thread doesn't work.

Please help me to resolve this issue. Is there any alternative for this.

Please find the sample code below,

protected void Application_Start()
    {
        Thread thread = new Thread(new ThreadStart(ThreadFunc));
        thread.IsBackground = true;
        thread.Name = "ThreadFunc";
        thread.Start();
    }

    protected void ThreadFunc()
    {
        System.Timers.Timer t = new System.Timers.Timer();
        t.Elapsed += new System.Timers.ElapsedEventHandler(TimerWorker);
        t.Interval = 10000;
        t.Enabled = true;
        t.AutoReset = true;
        t.Start();
    }

    protected void TimerWorker(object sender, System.Timers.ElapsedEventArgs e)
    {
        //work args
    }

Thanks,

Vijay


Solution

  • I don't think spawning a thread in global.asax is a good idea. Please see this question for better solutions.