Search code examples
c#datetimepickertimed-events

Setting DateTimePicker closes application only when triggered from timed action


I have a function in C# which, at the outset, sets the value of a GUI DateTimePicker object to today's date (time = midnight), then does other stuff. When executed via GUI button, the function (DBIO_Morning) runs fine. But, executed via timed action:

private void SetupTimedActions()
{
   ...

   DateTime ref_morning = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 8, 16, 0);
   if (DateTime.Now < ref_morning)
      At.Do(() => DBIO_Morning(), ref_morning);
   ...
}

it fails in the second line:

private void DBIO_Morning()
{
   DateTime date_current = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 0, 0, 0);
   DTPicker_start.Value = date_current;
   ...
}

( At.Do object is from the third answer here: C# execute action after X seconds )


Solution

  • Controls are not thread-safe, meaning you must not call a control's methods from another thread. You can wait until the control's thread is ready to handle your action using Control.Invoke:

    private void DBIO_Morning()
    {
        DateTime date_current = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 0, 0, 0);
        Action setValue = () => DTPicker_start.Value = date_current;
        if (DTPicker_start.InvokeRequired)
            DTPicker_start.Invoke(setValue);
        else
            setValue();
    }