Avatar of Cony TN
Cony TN
Flag for Germany asked on

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

Cony
ASP.NETC#Web Development

Avatar of undefined
Last Comment
Daniel Van Der Werken

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Daniel Van Der Werken

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes