写的程序要让用户计划何时运行一个函数。比如每周日晚8点,或每月第一天晚八点,等等。
类似于windows的计划任务。
查过前面一些帖子,我不是要在windows计划任务里调用我的程序
而是在我的程序里定时定点调用一个函数
用timer会不会太浪费资源了?
还有就是如果有现成成熟的控件,可以有类似于计划任务的效果,就更好了。
3x
类似于windows的计划任务。
查过前面一些帖子,我不是要在windows计划任务里调用我的程序
而是在我的程序里定时定点调用一个函数
用timer会不会太浪费资源了?
还有就是如果有现成成熟的控件,可以有类似于计划任务的效果,就更好了。
3x
解决方案 »
- 底层HTTP协议通信问题
- 请问datagridview里面的datagridviewcheckboxcolumn列里面的checkbox后面可以跟文字吗?
- 函数返回 Exception 之后该如何处理
- 请教个 泛型方法 泛型委托 匿名方法 扩展方法 lambda表达式 相关的一个难题。
- 更新数据SqlCommand SqlDataAdapter 用什么好?
- 为已经绑定数据源的dataGrid控件,设置新的Style出现错误???
- together for .net 与 vss的问题。
- 为何用HttpWebRequest下载页面时总是到中途停在那里
- 怎么能比较快比较两个不同的类的相同属性的值是否相等?
- 例子
- 简单问题,请教
- 简单一问,顶者有分。
有没有这种控件:
设置一个time,包含每周日晚8点,或每月第一天晚八点等信息
然后有 Ontime事件
到了触发点,我就ontime一下。
工具->文件夹工具->备份远程文件夹
然后点计划...
弹出计划窗口
按开始后,计划出现在下方项目条里,到时间就会自动运行了C#里如何实现呢?
{
void Execute();
}如前所述,我们的例子是实现往文件写如字符日期,下面就是实现这一任务的方法: public class SampleJob : ISchedulerJob
{
public void Execute()
{
//文件保存的物理路径,CSTest为虚拟目录名称,F:\Inetpub\wwwroot\CSTest为物理路径
string p = @"F:\Inetpub\wwwroot\CSTest";
//我们在虚拟目录的根目录下建立SchedulerJob文件夹,并设置权限为匿名可修改,
//SchedulerJob.txt就是我们所写的文件
string FILE_NAME = p+ "\\SchedulerJob\\SchedulerJob.txt";
//取得当前服务器时间,并转换成字符串
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);
}
}
}
}所有的准备工作已经完成,下面就是激活引擎的工作了。为了让我们的任务计划执行,我们在Global.asax.cs文件里的Applicatio_Start和Application_End里进行建立和销毁工作,首先建立一个调度进程运行的线程,我们这里的运行间隔时间为3秒钟。 public System.Threading.Thread schedulerThread = null;
protected void Application_Start(Object sender, EventArgs e)
{
SchedulerConfiguration config = new SchedulerConfiguration(1000*3);
config.Jobs.Add(new SampleJob());
Scheduler scheduler = new Scheduler(config);
System.Threading.ThreadStart myThreadStart = new System.Threading.ThreadStart(scheduler.Start);
System.Threading.Thread schedulerThread = new System.Threading.Thread(myThreadStart);
schedulerThread.Start();
}最后还需要在程序退出时进行销毁: protected void Application_End(Object sender, EventArgs e)
{
if (null != schedulerThread)
{
schedulerThread.Abort();
}
}好了,在VS.NET里建立一个C#的Web应用程序工程,建立TaskScheduler.cs类,并修改相应的Global.asax.cs文件。为了能看到效果,我们再建立一个表单WebForm1.aspx,定时刷新来检查我们所记录的数据: <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="CSTest.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>在Web应用程序中执行计划任务的例子</title>
<meta http-equiv="refresh" content="10">
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<iframe style="width:100%;height:100%" src="SchedulerJob/SchedulerJob.txt"></iframe>
</form>
</body>
</HTML>对工程进行编译并运行,就可以看到结果了,结果如下: 计划任务测试开始:
2003-13-10 11:08:15
2003-13-10 11:08:18
2003-13-10 11:08:21
2003-13-10 11:08:24
2003-13-10 11:08:27
2003-13-10 11:08:30需要说明的是,以上只是在Web应用程序中执行计划任务的简单例子,对于多个任务来说,需要在不同的线程内进行工作,对计划的安排也是很简单的,实际还需要站点堵塞,当机的情况。另外这里也没有进行错误的处理等工作,相信大家会写出更加完美的代码的。
而 不是 在程序里用timer,每隔几秒钟判断一下是否是我设定的时间.