C#定时执行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#定时执行相关的知识,希望对你有一定的参考价值。
在listview有很多任务,进行数据的抽取或导入,有的任务是每隔一段时间执行,有的是定时执行的
我现在想要启动程序的时候,就执行启动状态的任务,要怎么写啊?
我现在只知道要用Timer,不过一点思路都没有,有没有高手指点一下啊,最好有点代码提示
请问boy0213,谢谢,我还想问个问题,任务中连接数据库的信息、文件存放路径、任务名称、定时方式和时间都让我保存在一个xml文件里了,用你说的这个方法还适用吗?
每隔一段时间执行:建立一个 List<Timer> 型变量
每添加一个任务,就:
变量名.Add(new Timer())
可用 变量名[索引] 的方法向数组一样访问。
并设置属性 Interval, Enabled = True ,
设定事件 变量名[索引].Tick+= 事件处理函数
(这个函数要预先写好(不同种任务用不同函数,或用参数区分))
定时执行 用一个Timer检查 DateTime.Now==定时的时间 就分情况执行不同任务(调用不同函数)。
-------------------------------------------------
补充:
List<Type>是一个泛型的类。
既然叫List,它有列表的作用,类似于数组,又优于数组:数组的元素个数必须是确定,而List<>不确定。而List可以与数组一样访问。
既然是泛型,里面可以存任意同类型的变量。的尖括号中Type就是你想要的类型。
-----------------------------------------
//Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Example_Task
public partial class Form1 : Form
List<Task> lt = new List<Task>(); //任务列表
List<Timer> cycleT=new List<Timer>(); //管理周期性任务
Timer timingT=new Timer(); //管理定时任务
enum condition cycle, timing ; //任务执行条件
public enum tType a, b, c ; //任务类型
tType type;
bool Condition;
public Form1()
InitializeComponent();
timingT.Tick += new EventHandler(timingT_Tick);
timingT.Enabled = true;
void timingT_Tick(object sender, EventArgs e)
foreach (Task t in lt)
if (!t.Condition)
if (DateTime.Now == t.Time)
switch (t.Kind)
case tType.a: a(timingT, new EventArgs()); break;
case tType.b: b(timingT, new EventArgs()); break;
case tType.c: c(timingT, new EventArgs()); break;
void a(object sender,EventArgs e) label1.Text += 'a';
void b(object sender,EventArgs e) label1.Text += 'b';
void c(object sender,EventArgs e) label1.Text += 'c';
private void button1_Click(object sender, EventArgs e)
groupBox1.Visible = true;
private void tType_CheckedChanged(object sender, EventArgs e)
switch (((RadioButton)sender).Text)
case "a": type = tType.a; break;
case "b": type = tType.b; break;
case "c": type = tType.c; break;
private void Condition_CheckedChanged(object sender, EventArgs e)
numericUpDown1.Enabled = Condition = radioButton2.Equals(sender);
maskedTextBox1.Enabled = !Condition;
private void button3_Click(object sender, EventArgs e)
if (textBox1.Text != string.Empty)
try
if (Condition)
lt.Add(new Task(textBox1.Text, type, Condition, (int)numericUpDown1.Value));
Timer t=new Timer();
t.Interval=(int)numericUpDown1.Value*1000;
t.Enabled = true;
switch (type)
case tType.a: t.Tick += new EventHandler(a); break;
case tType.b: t.Tick += new EventHandler(b); break;
case tType.c: t.Tick += new EventHandler(c); break;
cycleT.Add(t);
else
lt.Add(new Task(textBox1.Text, type, Condition, DateTime.Parse(maskedTextBox1.Text)));
listBox1.Items.Add(textBox1.Text);
catch
finally
groupBox1.Hide();
public class Task
public string Name get; set;
public Form1.tType Kind get; set;
public bool Condition get; set;
public int sec get; set;
public DateTime Time get; set;
//定义各种属性描述这个任务
public Task(string name, Form1.tType kind, bool condition, int seconds)
Name = name; Kind = kind; Condition = condition; sec = seconds;//初始化
public Task(string name, Form1.tType kind, bool condition, DateTime d)
Name = name; Kind = kind; Condition = condition; Time = d;//初始化
//-------------------------------------------------
//-------------------------------------------------
//-------------------------------------------------
//Form1.Designer.cs
namespace Example_Task
partial class Form1
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
if (disposing && (components != null))
components.Dispose();
base.Dispose(disposing);
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
this.label1 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button3 = new System.Windows.Forms.Button();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.label3 = new System.Windows.Forms.Label();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.radioButton5 = new System.Windows.Forms.RadioButton();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.maskedTextBox1 = new System.Windows.Forms.MaskedTextBox();
this.groupBox1.SuspendLayout();
this.groupBox3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(12, 137);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(590, 334);
this.label1.TabIndex = 0;
this.label1.Text = "任务运行情况 - ";
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(12, 9);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(238, 124);
this.listBox1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(258, 23);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 97);
this.button1.TabIndex = 2;
this.button1.Text = "New Task";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Controls.Add(this.groupBox3);
this.groupBox1.Controls.Add(this.groupBox2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Location = new System.Drawing.Point(339, 9);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(263, 125);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "TaskInfo";
this.groupBox1.Visible = false;
//
// button3
//
this.button3.Location = new System.Drawing.Point(172, 12);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 4;
this.button3.Text = "OK";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.maskedTextBox1);
this.groupBox3.Controls.Add(this.radioButton3);
this.groupBox3.Controls.Add(this.label3);
this.groupBox3.Controls.Add(this.numericUpDown1);
this.groupBox3.Controls.Add(this.radioButton2);
this.groupBox3.Location = new System.Drawing.Point(74, 41);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(183, 78);
this.groupBox3.TabIndex = 3;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Condition";
//
// radioButton3
//
this.radioButton3.AutoSize = true;
this.radioButton3.Location = new System.Drawing.Point(6, 35);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(89, 16);
this.radioButton3.TabIndex = 3;
this.radioButton3.TabStop = true;
this.radioButton3.Text = "CertainTime";
this.radioButton3.UseVisualStyleBackColor = true;
this.radioButton3.CheckedChanged += new System.EventHandler(this.Condition_CheckedChanged);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(122, 18);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(47, 12);
this.label3.TabIndex = 2;
this.label3.Text = "Seconds";
//
// numericUpDown1
//
this.numericUpDown1.Enabled = false;
this.numericUpDown1.Location = new System.Drawing.Point(60, 13);
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size(61, 21);
this.numericUpDown1.TabIndex = 1;
this.numericUpDown1.Value = new decimal(new int[]
1,
0,
0,
0);
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(6, 14);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(53, 16);
this.radioButton2.TabIndex = 0;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "Every";
this.radioButton2.UseVisualStyleBackColor = true;
this.radioButton2.CheckedChanged += new System.EventHandler(this.Condition_CheckedChanged);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.radioButton5);
this.groupBox2.Controls.Add(this.radioButton4);
this.groupBox2.Controls.Add(this.radioButton1);
this.groupBox2.Location = new System.Drawing.Point(6, 41);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(63, 78);
this.groupBox2.TabIndex = 2;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Type";
//
// radioButton5
//
this.radioButton5.AutoSize = true;
this.radioButton5.Location = new System.Drawing.Point(6, 56);
this.radioButton5.Name = "radioButton5";
this.radioButton5.Size = new System.Drawing.Size(29, 16);
this.radioButton5.TabIndex = 2;
this.radioButton5.TabStop = true;
this.radioButton5.Text = "c";
this.radioButton5.UseVisualStyleBackColor = true;
this.radioButton5.CheckedChanged += new System.EventHandler(this.tType_CheckedChanged);
//
// radioButton4
//
this.radioButton4.AutoSize = true;
this.radioButton4.Location = new System.Drawing.Point(6, 38);
this.radioButton4.Name = "radioButton4";
this.radioButton4.Size = new System.Drawing.Size(29, 16);
this.radioButton4.TabIndex = 1;
this.radioButton4.TabStop = true;
this.radioButton4.Text = "b";
this.radioButton4.UseVisualStyleBackColor = true;
this.radioButton4.CheckedChanged += new System.EventHandler(this.tType_CheckedChanged);
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(6, 16);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(29, 16);
this.radioButton1.TabIndex = 0;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "a";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.CheckedChanged += new System.EventHandler(this.tType_CheckedChanged);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(53, 14);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(18, 17);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 0;
this.label2.Text = "Name";
//
// maskedTextBox1
//
this.maskedTextBox1.Enabled = false;
this.maskedTextBox1.Location = new System.Drawing.Point(6, 51);
this.maskedTextBox1.Mask = "0000-00-00 90:00:00";
this.maskedTextBox1.Name = "maskedTextBox1";
this.maskedTextBox1.Size = new System.Drawing.Size(143, 21);
this.maskedTextBox1.TabIndex = 4;
this.maskedTextBox1.ValidatingType = typeof(System.DateTime);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(614, 480);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.RadioButton radioButton4;
private System.Windows.Forms.RadioButton radioButton5;
private System.Windows.Forms.MaskedTextBox maskedTextBox1;
/*
没有太多的容错方法,为了简洁,尽量直达目的。
这个窗体可以定时或周期性执行任务
仔细看看Form1.cs
有点多线程的思路,但这里没有用多线程。
只要改写a(),b(),c() 就能完成你的任务,用同样的方法添加函数对应不同任务种类。
这下改明白了吧
多加点分吧
*/ 参考技术A 没有什么代码,放上timer之后,双击timer,进入timer的事件,在这里写你要执行的代码就可以。
timer的属性设置一下,一个是间隔时间,另一个是Enabled设置为true(这个可以根据需要在代码里设置,设置为true的时候就开始起作用了,false的时候停止) 参考技术B “每隔一段”和“定时”?
定时可以用Timer
每隔一段这个功能要用stopwatch了吧 参考技术C 不知道你究竟要问什么,是设计,还是技术问题。
启动程序,先去数据库里查,有没有未完成的任务,如果有,启动任务,如果没有,等待; 参考技术D 放几个Timer
timer事件中写你的方法
启用就让timer.start();
C#如何在BackgroundWorker 后台线程中使用定时器?
就是说我有一项任务要用BackgroundWorker来进行异步操作,并且需要定时进行,由于定时时间比较短,只有1秒钟,所以不能使用System.Windows.Forms名字空间中的Timer来定时执行,因为它是主线程的定时器,每秒执行一次异步操作,会导致窗体反应迟钝甚至出现未响应的情况,而其他Timer怎么用呢?捣鼓好多天了都不行,那位C#高手帮帮忙,给100分
实际上,Timer并不只有你说的System.Windows.Forms.Timer一种。这个东西作为一个主线程(即UI线程)的控件,它的执行都会在UI线程,当然会造成线程阻塞,也就是你说的窗体未响应。你可以尝试使用System.Threading.Timer,它可以实现控件Timer的一切功能。另外,和你说的System.Widnows.Forms.Timer不一样的是,它可以定义在非主线程中,当然它的定时执行方法也就不会影响到主线程了。
使用方法如下。
System.Threading.Timer timer = new System.Threading.Timer();//初始化
timer.Interval = 1000; //定时间隔1000ms
timer.Elapsed+=new EventHandler(timer_Elapsed);
private void timer_Elapsed()
//这里的代码每隔1秒时间就会运行,写你定时执行的事件即可
//并且,每次代码执行到这里,其实.NET内部都是用新的线程来处理这部分代码的
你将上面的东西封装成一个方法,注册到BackgroundWorker中,UI就不会假死的。
实际上,要在UI上定时调用异步,并且解决UI的假死,还有其他的方法,BackgroundWorker并不是唯一的选择。 参考技术A Timer会导致窗体反应异常?
可能是你代码的问题吧?
你把代码给我看看?
其它的Timer跟Forms的Timer其实基本差不多
你也可以尝试用Thread执行一个无限循环的方法,比如:
while(true)
做事的代码
Thread.Sleep(1000);// 休眠一秒
本回答被提问者采纳
以上是关于C#定时执行的主要内容,如果未能解决你的问题,请参考以下文章