c# - .NET Timer - Resource cleanup -


How the resource used by the timer in C # has been released, tried the following methods, The application is not working.

  T. translation (); T.Stop (); T.EndInit (); T.Close ();  

How do you know that it does not work?
Removal () should work. Can you still use the timer (if you should not do it right)?
If you want to delete the memory then you have to call GC. Compiler ()

The timer in a user statement (if you do not need a timer):

 using  (timer t = new timer ()) {// content With T}  

timer.devase () will be automatically said.


Here is an example of timer disposal:

  constant zero timestest () {usage (timer t = new timer (200)) {t. Start (); T.Elapsed + = New ElapsedEventHandler (t_Elapsed); System.Threading.Thread.Sleep (5000); // some work here} system. threading. Thread Sleep (5000); // Wait before application is closed} Fixed zero t_Elapsed (Object Sender, ElapsedEventArgs E) {Console.WriteLine ("Hello."); }  

Call it in your main () method. This will print "Hello". For 5 seconds, the timer is settled and t_Elapsed will not be asked again after 5 seconds of exit the app.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -