Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置DateTimePicker仅在定时操作触发时关闭应用程序_C#_Datetimepicker_Timed Events - Fatal编程技术网

C# 设置DateTimePicker仅在定时操作触发时关闭应用程序

C# 设置DateTimePicker仅在定时操作触发时关闭应用程序,c#,datetimepicker,timed-events,C#,Datetimepicker,Timed Events,我在C中有一个函数,它在开始时将GUI DateTimePicker对象的值设置为今天的date time=midnight,然后执行其他操作。通过GUI按钮执行时,函数DBIO_Morning运行良好。但是,通过定时操作执行: private void SetupTimedActions() { ... DateTime ref_morning = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.T

我在C中有一个函数,它在开始时将GUI DateTimePicker对象的值设置为今天的date time=midnight,然后执行其他操作。通过GUI按钮执行时,函数DBIO_Morning运行良好。但是,通过定时操作执行:

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

At.Do对象来自这里的第三个答案:

控件不是线程安全的,这意味着您不能从另一个线程调用控件的方法。您可以等待控件的线程准备好使用控件处理您的操作。调用:


您正试图从另一个线程修改GUI元素,该线程由At.Do隐式创建。看


在At.Do中使用System.Windows.Forms.Timer而不是System.Threading.Timer可能会解决此问题。只需更换新的计时器。。。到新的System.Windows.Forms.Timer…

实际上只是将System.Threading.Timer更改为System.Windows.Forms.Timer不起作用,因为System.Windows.Forms.Timer没有4参数重载。但是,一般来说,这可能是更方便的修复,因为只有计时功能受到影响。您知道如何使System.Windows.Forms.Timer调用复制新系统的功能吗?Threading.TimerTimerTimerCallback Timer,Action Action,int DelayMillimes,int interval?他有一个例子。您可以创建一个计时器,设置其Tick事件和Interval属性,然后调用其Start方法。你没有这方面的单行构造函数,但它基本上是一样的。我以前浏览过这一页,但是如何使定时事件每天发生一次或在设定的时间一次性发生?我可以计算运行时和事件计划时间之间的时间差,并将其用作间隔,但如何防止事件执行在设置的间隔内重复?调用myTimer.Stop;从事件处理程序内部。请参见同一页中的示例。我测试了您的建议,它可以正常工作,但稍后我会在函数和计时器调用的其他函数中调用其他GUI。因此,我将尝试通过实现下面的第二个解决方案来限制对计时函数的更改,而不是更改每个相关的GUI调用。不过,谢谢!假设所有控件都在同一个线程中创建,您可以在单个控件中完成所有操作。调用时,您不必为每个方法调用创建单独的操作。不过,我同意,使用另一个计时器可能更容易,并且可以避免出现不需要计时器的线程。祝你好运。那代码有严重的问题。隐藏线程是一个非常糟糕的主意。垃圾收集使它消失得无影无踪,烤蛋糕。扔掉它,只需使用Winforms计时器。不要隐藏它。
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;
   ...
}
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();
}