Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i got this problem...

What would be a good solution for this?

 private void button1_Click(object sender, EventArgs e)
    {
        aTimer.Enabled = true;
        button1.Enabled = false;

    }

    private void button2_Click(object sender, EventArgs e)
    {
        aTimer.Enabled = false;
    }

    private void timer_is_working(object source, ElapsedEventArgs e)
    {
        aTimer.Enabled = false;
        button1.Enabled = true;
    }

Thanks! Kind regards Daniel Ruescher

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
243 views
Welcome To Ask or Share your Answers For Others

1 Answer

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; }));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...