start a timer from different thread in c#

后端 未结 4 1780
挽巷
挽巷 2020-12-16 15:37

Hi i have stepped into some problem related to timer. hope somebody can help..

  1. I have a windows form containing a button
  2. when i click on that button i
4条回答
  •  我在风中等你
    2020-12-16 15:58


    You could try to start the timer this way:

    Add in form constructor this:

    System.Timers.Timer aTimer = new System.Timers.Timer();
     aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
     // Set the Interval to 1 second.
     aTimer.Interval = 1000;
    

    Add this method to Form1:

     private static void OnTimedEvent(object source, ElapsedEventArgs e)
     {
       //do something with the timer
     }
    

    On button click event add this:

    aTimer.Enabled = true;
    

    This timer is already threaded so no need to start a new thread.

提交回复
热议问题