Global中使用线程隔时执行一项任务


None.gif 在Global中使用线程隔时执行一项任务 
None.gifGLOBAL中线程的应用(书中学习,整理了一下),以下是过5分钟删除一张表的记录
None.gif
using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  DataBase;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Web.Security;
None.gif
using  System.IO ;
None.gif
using  System.Threading;
None.gif
namespace  FreightCom 
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif 
/// 
InBlock.gif 
/// Global 的摘要说明。
ExpandedSubBlockEnd.gif 
/// 

InBlock.gif public class Global : System.Web.HttpApplication
ExpandedSubBlockStart.gif 
{
ExpandedSubBlockStart.gif  
/// 
InBlock.gif  
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  private System.ComponentModel.IContainer components = null;
InBlock.gif  
private OnLineUser ou=new OnLineUser();
InBlock.gif  
private UserLoginInfo Login=new UserLoginInfo();
InBlock.gif  
public Global()
ExpandedSubBlockStart.gif  
{
InBlock.gif   InitializeComponent();
ExpandedSubBlockEnd.gif  }
 
InBlock.gif  
InBlock.gif  
protected void Application_Start(Object sender, EventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   SchedulerAgent.StartAgent();
ExpandedSubBlockEnd.gif  }

InBlock.gif 
InBlock.gif  
protected void Session_Start(Object sender, EventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif  
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
protected void Application_BeginRequest(Object sender, EventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
protected void Application_EndRequest(Object sender, EventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
protected void Application_Error(Object sender, EventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
protected void Session_End(Object sender, EventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
protected void Application_End(Object sender, EventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   SchedulerAgent.Stop();
ExpandedSubBlockEnd.gif  }

InBlock.gif   
ContractedSubBlock.gif  
Web 窗体设计器生成的代码
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif 
public interface ISchedulerJob
ExpandedSubBlockStart.gif 
{
InBlock.gif  
void Execute();
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif 
public class SchedulerConfiguration
ExpandedSubBlockStart.gif 
{
InBlock.gif  
private int sleepInterval;
InBlock.gif  
private ArrayList jobs=new ArrayList();
InBlock.gif  
public int SleepInterval
ExpandedSubBlockStart.gif  
{
ExpandedSubBlockStart.gif   
get{return sleepInterval;}
ExpandedSubBlockEnd.gif  }

InBlock.gif  
public ArrayList Jobs 
ExpandedSubBlockStart.gif  
{
ExpandedSubBlockStart.gif   
get {return jobs;}
ExpandedSubBlockEnd.gif  }

InBlock.gif  
public SchedulerConfiguration(int newSleepInterval)
ExpandedSubBlockStart.gif  
{
InBlock.gif   sleepInterval
=newSleepInterval;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif 
InBlock.gif 
public class Scheduler
ExpandedSubBlockStart.gif 
{
InBlock.gif  
private SchedulerConfiguration configuration = null;
InBlock.gif  
public Scheduler(SchedulerConfiguration config)
ExpandedSubBlockStart.gif  
{
InBlock.gif   configuration
=config;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
public void Start()
ExpandedSubBlockStart.gif  
{
InBlock.gif   
while(true)
ExpandedSubBlockStart.gif   
{
InBlock.gif    
try
ExpandedSubBlockStart.gif    
{
InBlock.gif     
foreach(ISchedulerJob job in configuration .Jobs)
ExpandedSubBlockStart.gif     
{
InBlock.gif      job.Execute();
ExpandedSubBlockEnd.gif     }

InBlock.gif    
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gif    
catch{}
InBlock.gif    
finally
ExpandedSubBlockStart.gif    
{
InBlock.gif     Thread.Sleep(configuration.SleepInterval);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif 
public class SchedulerAgent
ExpandedSubBlockStart.gif 
{
InBlock.gif  
private static System.Threading.Thread schedulerThread=null;
InBlock.gif  
public static void StartAgent()
ExpandedSubBlockStart.gif  
{
InBlock.gif   SchedulerConfiguration config
=new SchedulerConfiguration(1000*300);//设置时间,此处为5分钟
InBlock.gif
   config.Jobs.Add(new AlertJob());
InBlock.gif   Scheduler scheduler
=new Scheduler(config);
InBlock.gif   System.Threading.ThreadStart myThreadStart
=new System.Threading.ThreadStart(scheduler.Start);
InBlock.gif   schedulerThread
=new System.Threading.Thread(myThreadStart);
InBlock.gif   schedulerThread.Start();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
public static void Stop()
ExpandedSubBlockStart.gif  
{
InBlock.gif   
if(null!=schedulerThread)
ExpandedSubBlockStart.gif   
{
InBlock.gif    schedulerThread.Abort();
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif 
public class AlertJob:ISchedulerJob
ExpandedSubBlockStart.gif 
{
InBlock.gif  
private OnLineUser ou=new OnLineUser();
InBlock.gif  
public void Execute()
ExpandedSubBlockStart.gif  
{
InBlock.gif   ou.UserDelOnline();
//一个删除的方法
ExpandedSubBlockEnd.gif
  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif




本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/10/12/253299.html,如需转载请自行联系原作者

你可能感兴趣的:(Global中使用线程隔时执行一项任务)