【发布时间】:2011-12-17 13:33:47
【问题描述】:
我在 C# 中有一个函数,它一开始将 GUI DateTimePicker 对象的值设置为今天的日期(时间 = 午夜),然后执行其他操作。通过 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);
...
}
第二行失败:
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 对象来自这里的第三个答案:C# execute action after X seconds)
【问题讨论】:
-
该代码存在严重问题。隐藏线程是一个非常糟糕的主意。一个垃圾收集器让它消失得无影无踪,烤蛋糕。扔掉它,只需使用 Winforms 计时器。不要隐藏它。
标签: c# datetimepicker timed-events