2013-12-15 39 views
3
var timer = new Timer 
{ 
    Enabled = true, 
    Interval = 500 
}; 
Debug.WriteLine("new timer created"); 
timer.Elapsed += (s, e) => 
{ 
    Debug.WriteLine("hello from timer"); 
    // this doesn't get shown 
    // in the output window 
} 

我需要做什麼才能在上面的輸出窗口中看到我最後的Debug.WriteLine()Debug.WriteLine不適用於不同的線程

+0

@ColeJohnson:No.1)標準輸出是過程全局。 2)這不是標準輸出。 – SLaks

+0

您是否有其他Debug.WriteLine消息出現在輸出窗口中? – Gusdor

+0

@Gusdor你的意思是除了第一個? – Johan

回答

1

實際上,您實際上並未使用單獨的線程,但如果UI消息泵可用,則該定時器運行在System.Timers計時器上。如果你阻塞了UI線程,定時器將永遠不會運行。

檢查UI線程未被阻止,use System.Threading.Timer that does not use the UI thread或將SynchronizingObject設置爲null,因此它將使用線程池線程而不是UI線程。

編輯:或者像Hans said in his comment,你正在控制檯中運行,並且Main()在定時器有機會觸發之前退出。

+0

這不是。該事件被命名爲Elapsed,而不是Tick。 –

+0

@HansPassant System.Timers有同樣的問題。編輯帖子。 –

相關問題