|
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Data. SqlClient;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Namespace csdn
{
/// <Summary>
/// Summary of WebForm54.
/// </Summary>
Public class WebForm54: System. Web. UI. Page
{
Protected System. Web. UI. HtmlControls. HtmlGenericControl div_load;
Protected System. Web. UI. WebControls. Button btn_startwork;
Protected System. Web. UI. WebControls. Label lab_state;
Protected System. Web. UI. WebControls. Label lab_jg;
Protected work w;
Private void Page_Load (object sender, System. EventArgs e)
{
// Place user code here to initialize the page
If (Session ["work"] = null)
{
W = new work ();
Session ["work"] = w;
}
Else
{
W = (work) Session ["work"];
}
Switch (w. State)
{
Case 0:
{
This. div_load.Visible = false;
Break;
}
Case 1:
{
This. lab_state.Text = "" + (TimeSpan) (DateTime. now-w.StartTime )). totalSeconds. toString ("0.00") + "seconds passed, completion percentage:" + w. percent + "% ";
This. btn_startwork.Enabled = false;
Page. RegisterStartupScript ("", "<script> window. setTimeout ('location. href = location. href ', 1000); </script> ");
This. lab_jg.Text = "";
Break;
}
Case 2:
{
This. lab_jg.Text = "The task is finished and all operations are successfully performed, with" + (TimeSpan) (w. FinishTime-w.StartTime). TotalSeconds + "seconds ";
This. btn_startwork.Enabled = true;
This. div_load.Visible = false;
Break;
}
Case 3:
{
This. lab_jg.Text = "task ended, with an error in" + (TimeSpan) (w. ErrorTime-w.StartTime). TotalSeconds + "seconds leading to task failure '";
This. btn_startwork.Enabled = true;
This. div_load.Visible = false;
Break;
}
}
}
# Code generated by region Web form designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. btn_startwork.Click + = new System. EventHandler (this. btn_startwork_Click );
This. Load + = new System. EventHandler (this. Page_Load );
}
# Endregion
Private void btn_startwork_Click (object sender, System. EventArgs e)
{
If (w. State! = 1)
{
This. btn_startwork.Enabled = false;
This. div_load.Visible = true;
W. runwork ();
Page. RegisterStartupScript ("", "<script> location. href = location. href; </script> ");
}
}
}
Public class work
{
Public int State = 0; // 0-not started, 1-running, 2-successful, 3-failed
Public int Percent = 0; // percentage of completion
Public DateTime StartTime;
Public DateTime FinishTime;
Public DateTime ErrorTime;
Public void runwork ()
{
Lock (this)
{
If (State! = 1)
{
State = 1;
StartTime = DateTime. Now;
System. Threading. Thread thread = new System. Threading. Thread (new System. Threading. ThreadStart (dowork ));
Thread. Start ();
}
}
}
Private void dowork ()
{
Try
{
SqlConnection conn = new SqlConnection (System. Configuration. ConfigurationSettings. Deleettings ["conn"]);
SqlCommand cmd = new SqlCommand ("Insert Into test (test) values ('test')", conn );
Conn. Open ();
For (int p = 0; p <100; p ++)
{
For (int I = 0; I <10; I ++)
{
Cmd. ExecuteNonQuery ();
}
Percent = p; // The percentage is defined here. You can estimate how much time the operation fee is defined as a percentage.
}
Conn. Close ();
// Execute a time-consuming database operation with the above code
State = 2;
}
Catch
{
ErrorTime = DateTime. Now;
Percent = 0;
State = 3;
}
Finally
{
FinishTime = DateTime. Now;
Percent = 0;
}
}
}
}
Time-consuming operations in network management are often repetitive operations. For example, to generate a static page, you often need to refresh the page periodically. This type of task is automatically executed by the program to meet our requirements.
Use multithreading in ASP. NET
Code
// Scheduled tasks of the program. Each task implements ISchedulerWork.
Public interface ISchedulerWork
{
Void Execute ();
}
// Assessment module -- assessment of personal notification email tasks
Public class EmaiSendingJob: ISchedulerWork
{
Public void Execute ()
{
;
}
}
// Create a configuration object to store the tasks to be periodically executed and the execution interval.
Public class SchedulerConfiguration
{
// Execute once every day at intervals
Private int sleepInterval;
// Task list
Private ArrayList jobs = new ArrayList ();
Public int SleepInterval {get {return sleepInterval ;}}
Public ArrayList Jobs {get {return jobs ;}}
// Constructor of the scheduling configuration class
Public SchedulerConfiguration (int newSleepInterval)
{
SleepInterval = newSleepInterval;
}
}
// Scheduling class, scheduled to execute the task of the configuration object
Public class Scheduler
{
Private SchedulerConfiguration configuration = null;
Public Scheduler (SchedulerConfiguration config)
{
Configuration = config;
}
Public void Start ()
{
While (true)
{
// Execute each task
Foreach (ISchedulerWork job in configuration. Jobs)
{
Thread myThread = new Thread (new ThreadStart (job. Execute ));
MyThread. Start ();
Thread. Sleep (configuration. SleepInterval );
}
}
}
} |