Windows form closes after System.Threading.Timer elaples

Oleksii Kozak

I'm building an alarm clock application and using System.Threading.Timer. After the timer is elapsed I wanna show usercontrol to allow user to choose either to wake up or to snooze. After adding a new control or form in callback the form is just closing.

private void setTimerButton_Click(object sender, EventArgs e)
    {
        try
        {
            alarmDate = setAlarmDateTimePicker.Value - DateTime.Now;
            var alarmClock = new System.Threading.Timer(AlarmCallback, null, alarmDate, TimeSpan.Zero);
            addedAlarmTextBox.Text = setAlarmDateTimePicker.Text;
        }
        catch
        {}
        finally
        {
            setAlarmDateTimePicker.Value = DateTime.Now;
        }
    }
    private void AlarmCallback(object state)
    {
            this.Controls.Add(new AlarmBeepsForm());
    }

As far as I know, it has something with the threads, but I'm quite new to programming and I don't understand what's wrong. How can I change the code to successfully add the control or should I use another type of timer?

jtxkopt

Unless you set Control.CheckForIllegalCrossThreadCalls to false, you should call Control.Invoke to switch to the thread that created the control handle.

private void setTimerButton_Click(object sender, EventArgs e)
{
   try
   {
      alarmDate = setAlarmDateTimePicker.Value - DateTime.Now;
      var alarmClock = new System.Threading.Timer(AlarmCallback, null, alarmDate, TimeSpan.Zero);
      addedAlarmTextBox.Text = setAlarmDateTimePicker.Text;
   }
   catch
   {}
   finally
   {
      setAlarmDateTimePicker.Value = DateTime.Now;
   }
}
private void AlarmCallback(object state)
{
    this.Invoke(new Action(() => this.Controls.Add(new AlarmBeepsForm()));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Timer that closes modal form in Windows Forms

System.Threading.Timer windows service memory leak

Is it possible that DateTime.UtcNow reports a wrong time after System.Threading.Timer elapses?

System.Timers.Timer vs System.Threading.Timer

Method inside System.Threading.Timer

System.Threading.Timer does not work correctly

Windows application closes after printing

How to stop timer event when child form closes in MDI project

windows command line windows closes after excution

System.Threading.Timer vs System.Threading.Thread.Sleep resolution - .NET Timer not using system clock resolution

Timer doesn't contain in System.Threading at Xamarin.Forms

How to preserve System.Threading.Timer when function is call again

Unit testing an async method that uses System.Threading.Timer

System.Threading.Timer won't work constantly

Replace Thread.Sleep with System.Threading.Timer?

How to call system.threading.timer on same thread?

System.Threading.Timer Calling an Implicit Duplicate of a Callback Function?

What happens when I use System.Threading.Timer with TimerTrigger?

System.Threading.Timer with WPF/C# problems

C# Issue while trying to use System.Threading.Timer

Using threading.Timer after a Request to my Flask Python Application

Timer to awake Windows System from Sleep after certain time (may be 5 minutes)

Selenium closes browser after Python code is finished filling out form

Timer Control in C# windows Form

How to interrupt threading timer?

Python threading timer issue

Strange Behavior with Threading and Timer

synchronous behavior of System.Windows.Forms.Timer

System.Windows.Forms.Timer not firing