0

The code below shows a timer that ticks every 100* 1000(milliseconds) to show a pop up message for registration.The below code is working but unfortunately my application gets hang after some time.

I have searched in google and stackoverflow for this answer.But i couldnt find a correct answer to make my application work without getting hanged.

        private System.Timers.Timer register_Timer = new System.Timers.Timer();

        register_Timer.Interval = (100 * 1000);
        register_Timer.Elapsed += new ElapsedEventHandler(register_Timer_Tick);     
        register_Timer.SynchronizingObject = this;            
        register_Timer.AutoReset = true;       
        register_Timer.Enabled = true;

        System.GC.KeepAlive(register_Timer);

        private void register_Timer_Tick(object sender, EventArgs e)
              {
                //Pop up to show register message
              }
Mothy
  • 406
  • 1
  • 7
  • 19
  • 1) That GC.KeepAlive isn't doing anything for you see http://stackoverflow.com/q/18136735/56778. 2) There's really no need to do math for the interval. `TimeSpan.FromSeconds(100).TotalMilliseconds` is more clear. Not a big deal when you're just doing 100 seconds, but if you wanted 27 minutes or 3 hours and 12 minutes, using `TimeSpan` is much more clear. – Jim Mischel Aug 09 '13 at 14:10
  • I think your application is hung for another reason. This code works OK for me. I think it is very like a `System.Windows.Forms.Timer`. Unless you have code somewhere else which may cause this problem. – King King Aug 09 '13 at 17:47
  • What kind of pop up are you showing? – Jim Mischel Aug 09 '13 at 20:11
  • @KingKing:If i dont use the timer, the application is not getting hanged. – Mothy Aug 10 '13 at 14:56
  • @JimMischel : Iam using a messagebox inside the timertick function. – Mothy Aug 10 '13 at 14:57

2 Answers2

6
    register_Timer.SynchronizingObject = this; 

This completely defeats the reason for using System.Timers.Timer. It prevents the Elapsed event handler from being raised on a threadpool thread, the property ensures it will run on the UI thread. Which is what you wanted.

But you still get all the disadvantages of that Timer class. Particularly its habit for swallowing exceptions without a diagnostic is very ugly. As well as continuing to raise the Elapsed event after the form is closed, ensuring this cannot happen is a very difficult problem to solve, there are two inherent race conditions. .NET 1.0 had some design mistakes related to threading, this was one of them.

Just don't do this, use a System.Windows.Forms.Timer instead. It will work exactly like your timer, minus all the disadvantages.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you for your reply.But i have also tried using the "System.Windows.Forms.Timer" before..Still the application gets hanged. :( – Mothy Aug 09 '13 at 12:34
  • 1
    Using System.Timers.Timer can't fix the original issue in your code. It is definitely smarter to ask *that* question instead. At least everybody can scratch-off a threading problem for whatever issue ails your program. Click the Ask Question button and be sure to post a snippet that reproduces the hang, this time using the synchronous timer. – Hans Passant Aug 09 '13 at 12:38
1

The application hangs because you're doing a popup (I assume a MessageBox or some other modal dialog box). Of course the application is going to hang. You're putting a modal dialog up in the UI thread.

The problem isn't with the timer, but with your application design.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Yes,Iam using a messagebox.Can you please tell me how can i make the application work without hanging?? – Mothy Aug 10 '13 at 14:58
  • @Mothy: To fix the problem, you have to use some other way to do the notification. You need a non-modal dialog, or to write information to a text box or something similar on the main form. – Jim Mischel Aug 12 '13 at 01:39