简体   繁体   中英

Using a timer to call a method of a form control C#, threading issues

I am building a custom control for a few of my forms which is an "indictor light"; an edit control that just changed color.

I would like to set a timer and see what the "light" looks like when it switches from state to state.

I have been reviewing this link C# Elapsed Timer MSDN

Though this does not work for me. I think the issue has to do with the Timer executing on another Thread meaning I cannot interact with the this (this.editControl.[...]) within the OnTimedEvent method.

Is there a simple Timer I could use to just call a method of a forms controls every second or so?

Use the Timer control (the System.Windows.Forms.Timer class). You can find it in the Toolbox when on a designer canvas.

Set the Interval property to 1000 (1 second) and make sure its Enabled . The Tick event fires at every interval and is raised in the UI thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM