我在C#中有一個函數,它首先將GUI DateTimePicker對象的值設置爲今天的日期(時間=午夜),然後執行其他操作。當通過GUI按鈕執行時,函數(DBIO_Morning)運行良好。但是,通過定時操作執行:設置DateTimePicker僅在定時動作觸發時關閉應用程序
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 Timer。不要隱藏它。 – 2011-12-17 15:12:19