Process tasks using thread separation in Global

Source: Internet
Author: User

Sometimes we need to do some processing at intervals, and we can use threads in Web. Config to process it.
(I learned from the book and sorted it out. Below are the records for deleting a table in five minutes)
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Web;
Using System. Web. SessionState;
Using DataBase;
Using System. Data. SqlClient;
Using System. Web. Security;
Using System. IO;
Using System. Threading;
Namespace FreightCom
{
/// <Summary>
/// Summary of Global.
/// </Summary>
Public class Global: System. Web. HttpApplication
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. IContainer components = null;
Private OnLineUser ou = new OnLineUser ();
Private UserLoginInfo Login = new UserLoginInfo ();
Public Global ()
{
InitializeComponent ();
}

Protected void Application_Start (Object sender, EventArgs e)
{
SchedulerAgent. StartAgent ();
}
 
Protected void Session_Start (Object sender, EventArgs e)
{

}

Protected void Application_BeginRequest (Object sender, EventArgs e)
{

}

Protected void Application_EndRequest (Object sender, EventArgs e)
{

}

Protected void Application_AuthenticateRequest (Object sender, EventArgs e)
{

}

Protected void Application_Error (Object sender, EventArgs e)
{

}

Protected void Session_End (Object sender, EventArgs e)
{

}

Protected void Application_End (Object sender, EventArgs e)
{
SchedulerAgent. Stop ();
}

# Code generated by region Web Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. components = new System. ComponentModel. Container ();
}
# Endregion
}

Public interface ISchedulerJob
{
Void Execute ();
}

Public class SchedulerConfiguration
{
Private int sleepInterval;
Private ArrayList jobs = new ArrayList ();
Public int SleepInterval
{
Get {return sleepInterval ;}
}
Public ArrayList Jobs
{
Get {return jobs ;}
}
Public SchedulerConfiguration (int newSleepInterval)
{
SleepInterval = newSleepInterval;
}
}
 
Public class Scheduler
{
Private SchedulerConfiguration configuration = null;
Public Scheduler (SchedulerConfiguration config)
{
Configuration = config;
}
Public void Start ()
{
While (true)
{
Try
{
Foreach (ISchedulerJob job in configuration. Jobs)
{
Job. Execute ();
}

}
Catch {}
Finally
{
Thread. Sleep (configuration. SleepInterval );
}
}
}
}
Public class SchedulerAgent
{
Private static System. Threading. Thread schedulerThread = null;
Public static void StartAgent ()
{
SchedulerConfiguration config = new SchedulerConfiguration (1000*300); // set the time, which is 5 minutes.
Config. Jobs. Add (new AlertJob ());
Scheduler scheduler = new Scheduler (config );
System. Threading. ThreadStart myThreadStart = new System. Threading. ThreadStart (scheduler. Start );
SchedulerThread = new System. Threading. Thread (myThreadStart );
SchedulerThread. Start ();
}
Public static void Stop ()
{
If (null! = SchedulerThread)
{
SchedulerThread. Abort ();
}
}
}

Public class AlertJob: ISchedulerJob
{
Private OnLineUser ou = new OnLineUser ();
Public void Execute ()
{
Ou. UserDelOnline (); // A Delete Method
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.