Guest User

Untitled

a guest
May 16th, 2012
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. DispatcherTimer not firing in WPF Application
  2. Timer is initialized
  3. 'WpfApplication5TimerTest.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:WindowsMicrosoft.NetassemblyGAC_MSILPresentationFramework.Aerov4.0_4.0.0.0__31bf3856ad364e35PresentationFramework.Aero.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  4. Sample thread
  5. Sample thread
  6. Sample thread
  7. Sample thread
  8. Sample thread
  9. Sample thread
  10. The thread '<No Name>' (0x10b0) has exited with code 0 (0x0).
  11. Sample thread exiting!
  12.  
  13. using System;
  14.  
  15. namespace WpfApplication5TimerTest
  16. {
  17. static class Program
  18. {
  19. [STAThread]
  20. static void Main(string[] args)
  21. {
  22. AppObject = new App();
  23. AppObject.Run();
  24. }
  25.  
  26. public static App AppObject
  27. {
  28. get;
  29. private set;
  30. }
  31. }
  32. }
  33.  
  34. using System;
  35. using System.Threading;
  36. using System.Windows;
  37.  
  38. namespace WpfApplication5TimerTest
  39. {
  40. public partial class App : Application
  41. {
  42. protected override void OnStartup(StartupEventArgs e)
  43. {
  44. var sampleThread = new Thread(new ThreadStart(SampleThreadEntryPoint));
  45. sampleThread.Start();
  46. new MainWindow().Show();
  47. }
  48.  
  49. private void SampleThreadEntryPoint()
  50. {
  51. SingletonWithTimer.Initialize();
  52. while (!_shutdownEvent.WaitOne(1000))
  53. Console.WriteLine("Sample thread");
  54. Console.WriteLine("Sample thread exiting!");
  55. }
  56.  
  57. protected override void OnExit(ExitEventArgs e)
  58. {
  59. _shutdownEvent.Set();
  60. }
  61.  
  62. private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
  63. }
  64. }
  65.  
  66. using System;
  67. using System.Windows;
  68.  
  69. namespace WpfApplication5TimerTest
  70. {
  71. public partial class MainWindow : Window
  72. {
  73. public MainWindow()
  74. {
  75. InitializeComponent();
  76. }
  77.  
  78. private void Window_Closed(object sender, EventArgs e)
  79. {
  80. Program.AppObject.Shutdown();
  81. }
  82. }
  83. }
  84.  
  85. using System;
  86. using System.Windows.Threading;
  87.  
  88. namespace WpfApplication5TimerTest
  89. {
  90. public class SingletonWithTimer
  91. {
  92. private static SingletonWithTimer Instance
  93. {
  94. get
  95. {
  96. if (_instance == null)
  97. {
  98. _instance = new SingletonWithTimer();
  99. }
  100. return _instance;
  101. }
  102. }
  103.  
  104. public static void Initialize()
  105. {
  106. SingletonWithTimer.Instance._timer = new DispatcherTimer();
  107. SingletonWithTimer.Instance._timer.Interval = TimeSpan.FromSeconds(2);
  108. SingletonWithTimer.Instance._timer.Tick += new EventHandler(SingletonWithTimer.Instance.OnTimerTick);
  109. SingletonWithTimer.Instance._timer.Start();
  110. Console.WriteLine("Timer is initialized");
  111. }
  112.  
  113. private void OnTimerTick(object sender, EventArgs e)
  114. {
  115. Console.WriteLine("TimerTick");
  116. }
  117.  
  118. private static SingletonWithTimer _instance;
  119. private DispatcherTimer _timer = null;
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment