⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mymutloithread.aspx.cs

📁 ASP C#代码实例 适合初学人士学习使用
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Threading;

namespace Example_11_13
{
	/// <summary>
	/// Summary description for MyMutloiThread.
	/// </summary>
	public class MyMutloiThread : System.Web.UI.Page
	{
		public static string FILEPATH = "";

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			FILEPATH = Server.MapPath("MyJob\\MyJob.txt");					
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}

	public interface ISchedulerJob
	{
		void Execute();
	}

	public class SampleJob : ISchedulerJob
	{
		public void Execute()
		{
			//文件保存的物理路径,CSTest为虚拟目录名称,F:\Inetpub\wwwroot\CSTest为物理路径
			string p = @"G:\CSPrograms\Example_11_13";
			//我们在虚拟目录的根目录下建立SchedulerJob文件夹,并设置权限为匿名可修改,
			//SchedulerJob.txt就是我们所写的文件
			string FILE_NAME = p+ "\\MyJob\\MyJob.txt";
//			string FILE_NAME = MyMutloiThread.FILEPATH;
			//取得当前服务器时间,并转换成字符串
			string c = System.DateTime.Now.ToString("yyyy-mm-dd hh:MM:ss");
			//标记是否是新建文件的标量
			bool flag = false;
			//如果文件不存在,就新建该文件
			if (!File.Exists(FILE_NAME))
			{
				flag = true;
				StreamWriter sr = File.CreateText(FILE_NAME);
				sr.Close();
			}
			//向文件写入内容
			StreamWriter x = new StreamWriter(FILE_NAME,true,System.Text.Encoding.Default);
			if(flag) x.Write("计划任务测试开始:");
			x.Write("\r\n"+c);
			x.Close();
		}
	}

	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)
			{
				//执行每一个任务
				foreach(ISchedulerJob job in configuration.Jobs)
				{
					ThreadStart myThreadDelegate = new ThreadStart(job.Execute);
					Thread myThread = new Thread(myThreadDelegate);
					myThread.Start();
					Thread.Sleep(configuration.SleepInterval);
				}
			}
		}
	}



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -