Cancel and restart a scheduled job(Global.asax.cs) from another aspx Page (C#)

Cony TN
Cony TN used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Independent Consultant
Commented:
There is really no such thing as a global variable in .NET.

Any object you instantiate within a class is only going to be available in the scope of that class. The most you can get is a class-wide variable. So, anything instantiated in Global.asax.cs

You can't instantiate an object in one class and work on it in another class without passing the object between the classes -- even if you name if 'global_something', it's not really global.

Web pages don't hold state either. So, you have that extra added wrinkle. If it were me, I would do this:

1. Create a session state object and when I create the GlobalVar object in Application_Start(...) in Global.asax.cs, I would then stuff that object into the session state.

Session["GlobalVar"] = GlovalVar;

Open in new window



2. In the Page_Load(...) method of the AnotherPage.aspx.cs, I would initialize a GlobalVar object and assign the value from the session state to this new object. Something like:

GlobalVar globalVar = new GlobalVar();
globalVar = (GlobalVar)Session["GlobalVar"];

Open in new window


Then, work with the globalVar object and see what happens.

I hope this is helpful and works for you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial