【发布时间】:2012-11-09 16:37:27
【问题描述】:
在用户定义的类中,我有一个计时器,当我Timer.Enabled.时它不会启动
用户定义的类:
TSerialIndicator = public class
private
method TxTimerEvent(Sender:System.Object; e:System.EventArgs);
public
Txlight:Label;
Txtimer:System.Windows.Forms.Timer;
constructor(mform:Form);
method Transmit;
method Receive;
end;
这里是构造函数:
constructor TSerialIndicator(mform:Form);
begin
TxLight := new Label;
TxLight.AutoSize := false;
TxLight.BorderStyle := BorderStyle.FixedSingle;
TxLight.Location := new point(52,163);
TxLight.Width := 20;
TxLight.Height := 20;
mform.Controls.Add(TxLight);
TxTimer := new System.Windows.Forms.Timer;
TxTimer.Interval:=1;
TxTimer.Enabled:=false;
TxTimer.Tick += new System.EventHandler(@TxTimerEvent);
TxLight.BackColor := Color.Black;
end;
这是定义的传输方法:
method TSerialIndicator.Transmit;
begin
TxLight.BackColor := Color.Red;
if TxTimer.Enabled = false then
TxTimer.Enabled:=true;
end;
这里是定义的 TxTimerEvent:
method TSerialIndicator.TxTimerEvent(Sender:System.Object; e:System.EventArgs);
begin
TxLight.BackColor := Color.Black;
TxTimer.Enabled:=false;
end;
以下是它的创建和使用方式:
Slight := new TSerialIndicator(self);
Slight.Transmit;
当我从程序的其他部分调用 Transmit 时,它会做它的事情,但 TxTimerEvent 永远不会触发。我什至尝试过启动/停止它的方法。它仍然没有执行它的 Tick 事件。但是,我确实注意到,当我在构造函数中启用计时器时,它确实会触发 TxTimerEvent ONCE。
我做错了什么?
提前致谢,
【问题讨论】:
-
这看起来不像 C#...
-
@akatakritos,我最初有 50 个,但它仍然没有被解雇。因此,考虑到可能间隔太大,我将其设置为 1 以查看它是否与时间有关。如您所见,它仍然对我不起作用。
-
那是一毫秒,它可以迅速将颜色变为红色并变回黑色,速度如此之快,你没有注意到。
-
@akatakritos,无论时机如何,我都调试了程序以确保实际调用了它的事件。我用断点运行代码。我的程序从未遇到 TxTimerEvent 的断点。
-
Transmit 是在与调用构造函数的线程不同的线程上调用的吗?
标签: .net winforms timer delphi-prism oxygene