2011-12-07 29 views
17

我想了解爲什麼包含在SingletonWithTimer中的DispatcherTimer在下面的WPF應用程序中沒有觸發。我一直在研究這幾天,似乎無法達到它的底部。此應用程序是我正在嘗試修復的現有應用程序中減少的主要部分。該項目的啓動對象是WPFApplication5TimerTest.ProgramDispatcherTimer沒有在WPF應用程序中觸發

Timer is initialized 
'WpfApplication5TimerTest.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
Sample thread 
Sample thread 
Sample thread 
Sample thread 
Sample thread 
Sample thread 
The thread '<No Name>' (0x10b0) has exited with code 0 (0x0). 
Sample thread exiting! 

這是Program.cs中:

在控制檯的輸出會列出如下,因爲在輸出中未示出的詞「TimerTick」的問題是顯而易見的

using System; 

namespace WpfApplication5TimerTest 
{ 
    static class Program 
    { 
     [STAThread] 
     static void Main(string[] args) 
     { 
      AppObject = new App(); 
      AppObject.Run(); 
     } 

     public static App AppObject 
     { 
      get; 
      private set; 
     } 
    } 
} 

這是App.xaml.cs:

using System; 
using System.Threading; 
using System.Windows; 

namespace WpfApplication5TimerTest 
{ 
    public partial class App : Application 
    { 
     protected override void OnStartup(StartupEventArgs e) 
     { 
      var sampleThread = new Thread(new ThreadStart(SampleThreadEntryPoint));    
      sampleThread.Start(); 
      new MainWindow().Show(); 
     } 

     private void SampleThreadEntryPoint() 
     { 
      SingletonWithTimer.Initialize(); 
      while (!_shutdownEvent.WaitOne(1000)) 
       Console.WriteLine("Sample thread"); 
      Console.WriteLine("Sample thread exiting!"); 
     } 

     protected override void OnExit(ExitEventArgs e) 
     { 
      _shutdownEvent.Set(); 
     } 

     private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);   
    } 
} 

這是MainWindow.x aml.cs:

using System; 
using System.Windows; 

namespace WpfApplication5TimerTest 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void Window_Closed(object sender, EventArgs e) 
     { 
      Program.AppObject.Shutdown(); 
     } 
    } 
} 

這是SingletonWithTimer.cs:

using System; 
using System.Windows.Threading; 

namespace WpfApplication5TimerTest 
{ 
    public class SingletonWithTimer 
    { 
     private static SingletonWithTimer Instance 
     { 
      get 
      { 
       if (_instance == null) 
       { 
        _instance = new SingletonWithTimer(); 
       } 
       return _instance; 
      } 
     } 

     public static void Initialize() 
     { 
      SingletonWithTimer.Instance._timer = new DispatcherTimer(); 
      SingletonWithTimer.Instance._timer.Interval = TimeSpan.FromSeconds(2); 
      SingletonWithTimer.Instance._timer.Tick += new EventHandler(SingletonWithTimer.Instance.OnTimerTick); 
      SingletonWithTimer.Instance._timer.Start(); 
      Console.WriteLine("Timer is initialized"); 
     } 

     private void OnTimerTick(object sender, EventArgs e) 
     { 
      Console.WriteLine("TimerTick"); 
     } 

     private static SingletonWithTimer _instance; 
     private DispatcherTimer _timer = null; 
    } 
} 

回答

41

這是因爲你已經創建了另一個(非UI)線程DispatcherTimer。因此,它將與上的Dispatcher關聯,即線程,而不是UI線程上的那個線程。由於該線程上沒有任何運行Dispatcher,它永遠不會觸發。

要麼在UI線程上創建DispatcherTimer,要麼使用constructor overload,它允許您傳遞特定的Dispatcher以供使用。

+7

例如:'新的DispatcherTimer(DispatcherPriority.Background,Application.Current.Dispatcher)' –

+0

@Kevin Kalitowski,出色的答案。完美的作品。謝謝! – Elangesh

相關問題