We help IT Professionals succeed at work.
Get Started

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

Cony TN
Cony TN asked
on
212 Views
Last Modified: 2015-08-19
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
Independent Consultant
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE