问题
Within a user-defined class, I have a timer and it just won't start when I Timer.Enabled.
User-Defined Class:
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;
Here is the constructor:
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;
Here is Transmit method as defined:
method TSerialIndicator.Transmit;
begin
TxLight.BackColor := Color.Red;
if TxTimer.Enabled = false then
TxTimer.Enabled:=true;
end;
Here is TxTimerEvent as defined:
method TSerialIndicator.TxTimerEvent(Sender:System.Object; e:System.EventArgs);
begin
TxLight.BackColor := Color.Black;
TxTimer.Enabled:=false;
end;
Here is how it is created and used:
Slight := new TSerialIndicator(self);
Slight.Transmit;
When I call Transmit from some other part of the program, it does its thing, but TxTimerEvent won't fire at all ever. I even tried Start/Stop its methods. It still didn't execute its Tick Event. However, I did notice that when I do enable the timer from within the constructor it does fire TxTimerEvent ONCE.
What am I doing wrong?
Thanks in advance,
回答1:
With method names like "Transmit" and "Receive", it is likely that a thread is involved. Like the threadpool thread on which a SerialPort's DataReceived event runs. Or code that runs due to a System.Timers.Timer's Elapsed event. Etcetera.
Setting a System.Windows.Forms.Timer's Enabled property to true in a worker thread like that doesn't work, it is not a thread-safe class. It does what it normally does, creates a hidden window that uses Windows' SetTimer() method to get the Tick event to fire. But that window is created on a thread that doesn't pump a message loop. So Windows doesn't generate the WM_TIMER messages.
Use Control.Begin/Invoke() as necessary to ensure any code that's related to timers or controls runs on the UI thread.
来源:https://stackoverflow.com/questions/13312164/why-timer-system-windows-forms-timer-wont-start