2013-12-15 43 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有同样的问题。编辑帖子。 –

相关问题