Cancel and restart a scheduled job(Global.asax.cs) from another aspx Page (C#)
I'm starting a timer in the Global.asax.cs to achieve a scheduled job and everything just works fine.
How can I stop and restart the scheduled job from another aspx Page and resume it again?
I tried to implement the code in the stop and start button but it doesn't work.
I' m working on a windows 7 machine and using Visual Studio 2010 and IIS (Version 7.5.7600.16385)
Here is how my code looks like:
//GlobalVariables.cs
public static class GlobalVar
{
public static bool StopBackgroundWorker;
}
//AnotherPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GlobalVariables.StopBackgroundWorker = false;
Start_button.Enabled = false;
Stop_button.Enabled = true;
}
}
//Stopping the Backgroundwork from the AnotherPage Page with the stop-Button
protected void StopBackgroundWorker(object sender, EventArgs e)
{
GlobalVar.StopBackgroundWorker = true;
Start_button.Enabled = true;
Stop_button.Enabled = false;
GlobalVar.myTimer.Enabled = false;
GlobalVar.myTimer.Stop();
}
//Restarting the Backgroundwork from the AnotherPage with the start-Button
protected void ReStartBackgroundWorker(object sender, EventArgs e)
{
GlobalVar.StopBackgroundWorker = false;
Stop_button.Enabled = true;
Start_button.Enabled = false;
GlobalVar.myTimer.Enabled = true;
GlobalVar.myTimer.Start();
}
//In the Global.asax.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.IO;
using System.Timers;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.ComponentModel;
void Application_Start(object sender, EventArgs e)
{
GlobalVar.StopBackgroundWorker = false;
GlobalVar.myTimer = new System.Timers.Timer(2000);
GlobalVar.myTimer.AutoReset = false;
GlobalVar.myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
GlobalVar.myTimer.Enabled = true;
GlobalVar.myTimer.Start();
}
public void myTimer_Elapsed(object sender, ElapsedEventArgs e)
{
.....
if (GlobalVar.StopBackgroundWorker == false)
{
//Do some stuff here
}
else
{
System.Threading.Thread.Sleep(63000);
GlobalVar.myTimer.Start();
}
}
Could someone tell me what I'm doing wrong?
Thank you