Friday, 19 August 2016




Scheduler in Asp.net (DotNet World)

In Global.asmx Page
protected void Application_Start(object sender, EventArgs e) 

   Background worker = new Background(); 
   worker.DoWork += new Background.DoWorkEventHandler(worker_DoWork); 
   worker.RunWorker(null); 
   Application["worker"] = worker; 

  
void worker_DoWork(ref int progress, ref object _result, params object[] arguments) 

   while (true) 
   { 
      Thread.Sleep(100); 
      WebClient refresh = new WebClient(); 
      try 
      { 
            refresh.UploadString("http://XXX.XXX/", string.Empty); 
      } 
      catch (Exception ex) 
      { 
      } 
      finally 
      { 
         refresh.Dispose(); 
      } 
      string strdt = DateTime.Now.ToString("HH:mm"); 
      if (strdt == "07:15") 
      { 
            //Call Method Or Write a Code  
      } 
      progress++; 
   } 
}  





Create Class Background.cs
public class Background 

   Thread _innerThread = null; 
   #region Properties 
   public int Progress 
   { 
      get 
      { 
         return _progress; 
      } 

int _progress = 0; 
public object Result 

   get 
   { 
      return _result; 
   } 

object _result = null; 
public bool IsRunning 

   get 
   { 
   if (_innerThread != null) 
   { 
      return _innerThread.IsAlive; 
   } 
   return false; 


#endregion 
#region Events 
public delegate void DoWorkEventHandler(ref int progress,ref object _result, params object[] arguments); 
public event DoWorkEventHandler DoWork; 
#endregion 
public void RunWorker(params object[] arguments) 

   if (DoWork != null) 
   { 
      _innerThread = new Thread(() => 
      { 
         _progress = 0; 
         DoWork.Invoke(ref _progress, ref _result, arguments); 
         _progress = 100; 
}); 
_innerThread.Start(); 


}

No comments :