So you did not make it clear, but based on the ElapsedEventArgs
type it seems that timer_is_working
is the Elapsed
event of a System.Timers.Timer
instance.
Be aware that the .NET Framework Class Library includes four classes
named Timer, each of which offers different functionality:
- System.Timers.Timer: fires an event at regular intervals. The class is intended for use as a server-based or service
component in a multithreaded environment.
- System.Threading.Timer: executes a single callback method on a thread pool thread at regular intervals. The callback method is
defined when the timer is instantiated and cannot be changed. Like
the System.Timers.Timer class, this class is intended for use as a
server-based or service component in a multithreaded environment.
- System.Windows.Forms.Timer: a Windows Forms component that fires an event at regular intervals. The component is designed for use in a single-threaded environment.
- System.Web.UI.Timer: an ASP.NET component that performs asynchronous or synchronous web page postbacks at a regular interval.
If this is a Windows.Forms app, use a System.Windows.Forms.Timer
instead (you find it in Toolbox/Components). Its Tick
event is raised in the UI thread so can access your controls from there.
If you have a special reason to use the System.Timers.Timer
(eg. precision), you must wrap your access into an Invoke
call:
Invoke(new Action(() => { button1.Enabled = true; }));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…