Why timer (system.windows.forms.timer) won't start?

限于喜欢 提交于 2019-12-12 04:43:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!