System.Windows.Forms.Timer怎么Stop后Start就不起作用了?

hhhanxf 2003-10-22 04:17:41
我的程序要定时做一些比较费时的工作,我是在Timer的Tick事件发生的时候首先调用Stop,然后异步调用Delegate:

private void timerComm_Tick(object sender, System.EventArgs e)
{
// this.Invoke(this.timerHandler, new object[] {false});
this.timerComm.Stop();
this.commHandler.BeginInvoke(this.endComm, null);
}

然后在endComm指定的函数中,再将Timer启动:

private void EndComm(IAsyncResult ar)
{
this.endComm.EndInvoke(ar);
this.timerComm.Enabled = true;
// this.Invoke(this.timerHandler, new object[] {true});
}
可是我发现这个Timer在运行一次之后就再也不触发Tick事件了,怎么回事呢。
各位可能看见了,注释掉的代码是我的另一次尝试,我怕是跨线程的问题,可是发现还是不起作用。请各位高人指条明路。
...全文
337 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
SadMovie 2003-10-24
  • 打赏
  • 举报
回复
为什么在Timer对象的触发事件里停止再启动Timer对象呢?能说一下你程序这部分的工作思路吗?
通常Timer对象的启动安排在定时事件之外,其停止也只在完成工作后才在定时事件内判断
hhhanxf 2003-10-23
  • 打赏
  • 举报
回复
any body help me?
csharplove 2003-10-23
  • 打赏
  • 举报
回复
你用timer.enable=true,false应该没问题
hhhanxf 2003-10-22
  • 打赏
  • 举报
回复
我单独做了个程序,证明了还是不行,应该怎么做才是正确的呢?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TryBinding
{
public delegate void TestDelegate ();
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
private TestDelegate t;// = new TestDelegate(this.Test);

private AsyncCallback endTest;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.t = new TestDelegate(this.Test);
this.endTest = new AsyncCallback(this.EndTest);
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(4, 5);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "start";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
}

private void Test()
{
MessageBox.Show("Test is running");
}

private void EndTest (IAsyncResult ar)
{
this.timer1.Enabled = true;
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.timer1.Interval = 2000;
this.timer1.Enabled = true;
}

private void timer1_Tick(object sender, System.EventArgs e)
{
this.timer1.Enabled = false;
this.t.BeginInvoke(this.endTest, null);
}
}
}

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧