c# - 定时器不会以多线程形式触发

标签 c# multithreading winforms timer

我在表单中有一个 System.Timers.Timer 计时器。我还有一个从 RFID 设备读取的线程(函数:GetData())。我想用计时器限制线程的时间,但计时器没有触发。

System.Threading.Thread GetData;
System.Timers.Timer timer = new System.Timers.Timer();
int reverseCounter=1000;

    public CardDragMaifareFrm()
    {
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        timer.Interval = 10;
        timer.Enabled = true;
        timer.Start();
        GetData = new Thread(new ThreadStart(ReadCardData));
        GetData.Start();
    }
   void timer_Elapsed(object sender, EventArgs e)
   {
        if (reverseCounter > 0)
        {
            MessageBox.Show("hey");
            reverseCounter -= 1;
        }
        else
        {// some actions for terminating GetData thread}
    }

但我没有看到“嘿”消息...有人可以帮助我吗?谢谢

最佳答案

我使用了计算机工程的第一条规则:重新启动它,也许它会起作用"... ;-)

关于c# - 定时器不会以多线程形式触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36029625/

相关文章:

c# - 添加新行时防止滚动到 datagridview 中的选定行

winforms - 按下 ESC 时关闭 PrintPreviewDialog

c# - 如何检测 ActiveX 支持?

multithreading - std::atomic 和 std::mutex 的区别

c# - 单元测试 : Using another method to check the method under test ran correctly

java - Hibernate 中的 CUD 操作线程安全吗?

c++ - 终止在不同 QThread 中运行的 QProcess

c# - 如何处理 Windows 窗体中文本框的完全更改?

c# - WPF 工具包 :AutoCompleteBox does not move accordingly as the main Window

c# - 为什么我不能在 try block 中分配对象变量?