2010-09-06 64 views
0

我想在每個配置的時間間隔發送某種類型的listenbeat。我想用調度計時器發送它。發送定期數據的調度器定時器

Main() 
{ 
    create dispatcher timer; 

} 

void dispacthertimertick() 
{ 
// send heartbeat 
} 

我該如何保持主線程活着?

問候 拉朱

+0

爲什麼WPF標籤?這是一個WPF應用程序或... – 2010-09-06 13:07:14

回答

0

把它在你的App.xaml.cs.最好的地方 WPF中的應用程序負責設置消息循環,因此您不應該爲此擔心。如果你的App.xaml有ApplicationDefinition(這是默認值)的生成操作屬性時,它會發出這樣的啓動代碼(你可以看到使用反射):

[STAThread, DebuggerNonUserCode] 
public static void Main() 
{ 
    App app = new App(); 
    app.InitializeComponent(); 
    app.Run(); 
} 

您需要使用OnStartup:

protected override void OnStartup(StartupEventArgs e) 
    { 
     base.OnStartup(e); 

     // setup your timer here 
    } 

Console.Read()實際上是一種破解和不必要的,因爲可能沒有控制檯,就像在Windows窗體中一樣。