2011-12-07 24 views
17

Tôi đang cố gắng hiểu tại sao DispatcherTimer chứa trong phạm vi SingletonWithTimer không kích hoạt trong ứng dụng WPF sau đây. Tôi đã nghiên cứu điều này trong một vài ngày và dường như không thể đến đáy nó. Ứng dụng này là các phần thiết yếu của một ứng dụng hiện có mà tôi đang cố sửa chữa. Đối tượng khởi động của dự án này là WPFApplication5TimerTest.Program.DispatcherTimer không kích hoạt trong ứng dụng WPF

Sản lượng trong danh sách giao diện điều khiển như sau, vấn đề là điều hiển nhiên vì từ "TimerTick" không được hiển thị trong kết quả:

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! 

Đây là Program.cs:

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; 
     } 
    } 
} 

Đây là 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);   
    } 
} 

Đây là 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(); 
     } 
    } 
} 

Đây là 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; 
    } 
} 

Trả lời

41

Đó là bởi vì bạn đã tạo DispatcherTimer trên khác (phi UI) chủ đề. Do đó, nó sẽ được gắn với số Dispatcher trên rằng chuỗi, chứ không phải là chuỗi trên chuỗi giao diện người dùng. Vì không có gì đang chạy Dispatcher trên chủ đề đó, nó sẽ không bao giờ cháy.

Hoặc tạo DispatcherTimer trên chuỗi giao diện người dùng hoặc sử dụng constructor overload cho phép bạn chuyển vào một số Dispatcher cụ thể để sử dụng.

+7

Ví dụ: 'mới DispatcherTimer (DispatcherPriority.Background, Application.Current.Dispatcher) ' –

+0

@Kevin Kalitowski, câu trả lời tuyệt vời. Hoạt động hoàn hảo. Cảm ơn! – Elangesh

Các vấn đề liên quan