请教到设定的时间就自动执行方法,应该如何实现? 例如,我添加一个广播,设定了一个时间,到时间就执行一个方法来广播需要添加好多个广播那么我应该怎么实现呢??谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该是添加一个Timer来定时执行. 是使用TIMER控件吗?需要用到线程吗?? 定时执行操作private void timer1_Tick(object sender, EventArgs e){ if(DateTime.Now.Hour.ToString()=="11") { this.timer1.Stop(); //操作 }} 我现在用的:c#怎么做一个定时运行程序!!!用timer控件,检测时间后触发事件timer控件using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace NoticePad { /// <summary> /// 定时提醒的小工具。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.NumericUpDown ndHour; private System.Windows.Forms.NumericUpDown ndMinute; private System.Windows.Forms.Button btn_OK; private System.ComponentModel.IContainer components; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.ndHour = new System.Windows.Forms.NumericUpDown(); this.ndMinute = new System.Windows.Forms.NumericUpDown(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.btn_OK = new System.Windows.Forms.Button(); this.timer1 = new System.Windows.Forms.Timer(this.components); ((System.ComponentModel.ISupportInitialize)(this.ndHour)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.ndMinute)).BeginInit(); this.SuspendLayout(); // // ndHour // this.ndHour.Location = new System.Drawing.Point(6, 6); this.ndHour.Maximum = new System.Decimal(new int[] { 23, 0, 0, 0}); this.ndHour.Name = "ndHour"; this.ndHour.Size = new System.Drawing.Size(36, 21); this.ndHour.TabIndex = 0; // // ndMinute // this.ndMinute.Location = new System.Drawing.Point(84, 6); this.ndMinute.Maximum = new System.Decimal(new int[] { 59, 0, 0, 0}); this.ndMinute.Name = "ndMinute"; this.ndMinute.Size = new System.Drawing.Size(36, 21); this.ndMinute.TabIndex = 1; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(48, 12); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(17, 17); this.label1.TabIndex = 2; this.label1.Text = "时"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(132, 12); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(17, 17); this.label2.TabIndex = 3; this.label2.Text = "分"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(6, 31); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox1.Size = new System.Drawing.Size(276, 156); this.textBox1.TabIndex = 4; this.textBox1.Text = "写入提醒的内容......"; // // btn_OK // this.btn_OK.Location = new System.Drawing.Point(174, 6); this.btn_OK.Name = "btn_OK"; this.btn_OK.Size = new System.Drawing.Size(102, 19); this.btn_OK.TabIndex = 5; this.btn_OK.Text = "确定并隐藏"; this.btn_OK.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.btn_OK.Click += new System.EventHandler(this.btn_OK_Click); // // timer1 // this.timer1.Interval = 1; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(289, 194); this.Controls.Add(this.btn_OK); this.Controls.Add(this.textBox1); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.ndMinute); this.Controls.Add(this.ndHour); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "提醒工具"; ((System.ComponentModel.ISupportInitialize)(this.ndHour)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.ndMinute)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private System.DateTime dtDes; // 定时周期的检查是否到了提醒的时间了。 private void timer1_Tick(object sender, System.EventArgs e) { if (DateTime.Now > dtDes) { timer1.Enabled = false; this.Show(); this.Activate(); } } // 确定提醒的时间,然后隐藏该工具。 private void btn_OK_Click(object sender, System.EventArgs e) { dtDes = new DateTime( DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, (int)ndHour.Value, (int)ndMinute.Value, 0,0); timer1.Enabled = true; this.Hide(); } } } //完整的定时器程序public partial class Form1 : Form{ public Form1() { InitializeComponent(); Timer vTimer = new Timer(); vTimer.Interval = 1000; vTimer.Enabled = true; vTimer.Tick += new EventHandler(vTimer_Tick); Text = DateTime.Now.ToString(); } void vTimer_Tick(object sender, EventArgs e) { Text = DateTime.Now.ToString(); }} 关于C#中timer类 在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Timers.Timer类里 System.Windows.Forms.Timer是应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API SetTimer实现的。它的主要缺点是计时不精确,而且必须有消息循环,Console Application(控制台应用程序)无法使用。 System.Timers.Timer和System.Threading.Timer非常类似,它们是通过.NET Thread Pool实现的,轻量,计时精确,对应用程序、消息没有特别的要求。System.Timers.Timer还可以应用于WinForm,完全取代上面的Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。 例: 使用System.Timers.Timer类 System.Timers.Timer t = new System.Timers.Timer(10000);//实例化Timer类,设置间隔时间为10000毫秒; t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件; t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; public void theout(object source, System.Timers.ElapsedEventArgs e) { MessageBox.Show("OK!"); } 看看你的是什么情况 t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); 这个啊 急,急,急Response.Redirect如何将页面指定到iframe中???? 关于正则表达式 数据更新问题: 静态接口问题。 问一个关于简单打印的问题 为什么用C#做程序,“对话框编辑器”上的控件是灰的 求Base24的编码算法 c# 关于object类型转换 英文系统如何显示中文 给定一个日期和天数,计算出新的日期,不采用DateTime的相关函数。 要怎么解决啊 请教C#WinForm下的UDP代码(服务器和客户端) ♂C#编程方式获取dll文件中都有什么类和方法♀
{
if(DateTime.Now.Hour.ToString()=="11")
{
this.timer1.Stop();
//操作
}
}
c#怎么做一个定时运行程序!!!
用timer控件,检测时间后触发事件
timer控件
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace NoticePad
{
/// <summary>
/// 定时提醒的小工具。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.NumericUpDown ndHour;
private System.Windows.Forms.NumericUpDown ndMinute;
private System.Windows.Forms.Button btn_OK;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.ndHour = new System.Windows.Forms.NumericUpDown();
this.ndMinute = new System.Windows.Forms.NumericUpDown();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.btn_OK = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.ndHour)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.ndMinute)).BeginInit();
this.SuspendLayout();
//
// ndHour
//
this.ndHour.Location = new System.Drawing.Point(6, 6);
this.ndHour.Maximum = new System.Decimal(new int[] {
23,
0,
0,
0});
this.ndHour.Name = "ndHour";
this.ndHour.Size = new System.Drawing.Size(36, 21);
this.ndHour.TabIndex = 0;
//
// ndMinute
//
this.ndMinute.Location = new System.Drawing.Point(84, 6);
this.ndMinute.Maximum = new System.Decimal(new int[] {
59,
0,
0,
0});
this.ndMinute.Name = "ndMinute";
this.ndMinute.Size = new System.Drawing.Size(36, 21);
this.ndMinute.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(48, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(17, 17);
this.label1.TabIndex = 2;
this.label1.Text = "时";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(132, 12);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(17, 17);
this.label2.TabIndex = 3;
this.label2.Text = "分";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(6, 31);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(276, 156);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "写入提醒的内容......";
//
// btn_OK
//
this.btn_OK.Location = new System.Drawing.Point(174, 6);
this.btn_OK.Name = "btn_OK";
this.btn_OK.Size = new System.Drawing.Size(102, 19);
this.btn_OK.TabIndex = 5;
this.btn_OK.Text = "确定并隐藏";
this.btn_OK.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.btn_OK.Click += new System.EventHandler(this.btn_OK_Click);
//
// timer1
//
this.timer1.Interval = 1;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(289, 194);
this.Controls.Add(this.btn_OK);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.ndMinute);
this.Controls.Add(this.ndHour);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "提醒工具";
((System.ComponentModel.ISupportInitialize)(this.ndHour)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.ndMinute)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private System.DateTime dtDes;
// 定时周期的检查是否到了提醒的时间了。
private void timer1_Tick(object sender, System.EventArgs e)
{
if (DateTime.Now > dtDes)
{
timer1.Enabled = false;
this.Show();
this.Activate();
}
}
// 确定提醒的时间,然后隐藏该工具。
private void btn_OK_Click(object sender, System.EventArgs e)
{
dtDes = new DateTime(
DateTime.Today.Year,
DateTime.Today.Month,
DateTime.Today.Day,
(int)ndHour.Value,
(int)ndMinute.Value,
0,0);
timer1.Enabled = true;
this.Hide();
}
}
}
{
public Form1()
{
InitializeComponent();
Timer vTimer = new Timer();
vTimer.Interval = 1000;
vTimer.Enabled = true;
vTimer.Tick += new EventHandler(vTimer_Tick);
Text = DateTime.Now.ToString();
} void vTimer_Tick(object sender, EventArgs e)
{
Text = DateTime.Now.ToString();
}
}
1.定义在System.Windows.Forms里
2.定义在System.Threading.Timer类里
3.定义在System.Timers.Timer类里
System.Windows.Forms.Timer是应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API SetTimer实现的。它的主要缺点是计时不精确,而且必须有消息循环,Console Application(控制台应用程序)无法使用。
System.Timers.Timer和System.Threading.Timer非常类似,它们是通过.NET Thread Pool实现的,轻量,计时精确,对应用程序、消息没有特别的要求。System.Timers.Timer还可以应用于WinForm,完全取代上面的Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。
例:
使用System.Timers.Timer类
System.Timers.Timer t = new System.Timers.Timer(10000);//实例化Timer类,设置间隔时间为10000毫秒;
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件;
t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
public void theout(object source, System.Timers.ElapsedEventArgs e)
{
MessageBox.Show("OK!");
}
看看你的是什么情况
这个啊