C#定时执行

在listview有很多任务,进行数据的抽取或导入,有的任务是每隔一段时间执行,有的是定时执行的我现在想要启动程序的时候,就执行启动状态的任务,要怎么写啊?我现在只知道要... 在listview有很多任务,进行数据的抽取或导入,有的任务是每隔一段时间执行,有的是定时执行的
我现在想要启动程序的时候,就执行启动状态的任务,要怎么写啊?
我现在只知道要用Timer,不过一点思路都没有,有没有高手指点一下啊,最好有点代码提示
请问boy0213,谢谢,我还想问个问题,任务中连接数据库的信息、文件存放路径、任务名称、定时方式和时间都让我保存在一个xml文件里了,用你说的这个方法还适用吗?
展开
 我来答
boy0213
推荐于2016-11-14 · TA获得超过257个赞
知道小有建树答主
回答量:143
采纳率:0%
帮助的人:166万
展开全部
启动程序的时候,就执行启动状态的任务? 用 Loaded 事件呀。

每隔一段时间执行:建立一个 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() 就能完成你的任务,用同样的方法添加函数对应不同任务种类。

这下改明白了吧

多加点分吧
*/
a5202003
2009-07-24 · TA获得超过2249个赞
知道大有可为答主
回答量:1885
采纳率:75%
帮助的人:593万
展开全部
没有什么代码,放上timer之后,双击timer,进入timer的事件,在这里写你要执行的代码就可以。
timer的属性设置一下,一个是间隔时间,另一个是Enabled设置为true(这个可以根据需要在代码里设置,设置为true的时候就开始起作用了,false的时候停止)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
christiankula
2009-07-24 · TA获得超过1007个赞
知道小有建树答主
回答量:1275
采纳率:0%
帮助的人:0
展开全部
“每隔一段”和“定时”?
定时可以用Timer
每隔一段这个功能要用stopwatch了吧
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
小佳碧玉E0bc4
2009-07-24 · TA获得超过546个赞
知道小有建树答主
回答量:432
采纳率:0%
帮助的人:251万
展开全部
不知道你究竟要问什么,是设计,还是技术问题。

启动程序,先去数据库里查,有没有未完成的任务,如果有,启动任务,如果没有,等待;
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Na年花开
2009-07-24 · 超过54用户采纳过TA的回答
知道答主
回答量:435
采纳率:0%
帮助的人:220万
展开全部
放几个Timer
timer事件中写你的方法
启用就让timer.start();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(4)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式