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
Cony TNAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel Van Der WerkenIndependent ConsultantCommented:
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.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.