使用Timer EventHandler时遇到问题 [英] getting an issue while using Timer EventHandler

查看:112
本文介绍了使用Timer EventHandler时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sql查询将数据插入数据库中,同时在Form1_Load,Button等中执行该方法时一切正常,但是当我尝试在计时器事件处理程序中执行此方法时,计时器执行两次查询

我需要的是以下内容:-

1.为什么会这样
2.此问题的解决方案

谢谢,

问候,

Snake.您的计时器将一直触发直到停止为止.因此,请在事件的第一行停止计时器触发(它还具有是否会触发一次以上的属性,但可以肯定的是,在事件中我总是将其关闭).但是,由于您提供的信息不足,我正在猜测原因是什么.


尝试将您的方法移出计时器刻度事件

计时器MyTimer =  Timer();

MyTimer_Tick(对象发​​送者,EventArgs e)
{
MyTimer.Enabled =  false ;
DoMyAction();
}

无效 DoMyAction()
{
// 您在此处执行操作
MyTimer.Enabled =  true ;
} 


i was trying to insert data into database using sql query ,while executing the method in Form1_Load,Button,etc everything is going fine, but when i try to execute this method into the timer eventhandler, the timer execute the query 2 times

What do i need is the following:-

1.Why is this happening
2.A solution for this issue

Thanks,

Regards,

Snake.

解决方案

Well, I''m not sure if you want help, because if you did, you''d surely post some code ? Your timer will keep firing until you stop it. So, stop the timer firing as the first line of your event ( it also has a property as to if it will fire more than once, but I always turn them off in the event, to be sure ). But, I am guessing as to what the cause is, based on the lack of info you provided.


try moving your method out of the timer tick event

Timer MyTimer = new Timer();

MyTimer_Tick(object sender, EventArgs e)
{
MyTimer.Enabled = false;
DoMyAction();
}

void DoMyAction()
{
//do you actions here
MyTimer.Enabled = true;
}


这篇关于使用Timer EventHandler时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆