0

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

VJOY
  • 3,752
  • 12
  • 57
  • 90

2 Answers2

0

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

Community
  • 1
  • 1
MK.
  • 33,605
  • 18
  • 74
  • 111
0

If there is no activity on your site for a period of time, it is shut down automatically.

To circumvent this, google "Keep asp.net site alive"

But you'll find a bunch of duct-tape like hacks. I would change the architecture.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69