HI WELCOME TO KANSIRIS

timer c sharp

Leave a Comment
How do you know that it didn't work?
Dispose() should work. Can you still use the timer afterwards (you shouldn't if the it works fine)?
If you want to deallocate the memory you must call GC.Collect().
 t.Dispose();
t
.Stop();
t
.EndInit(); 
            t.Close();

Also it's recommended to wrap the timer in a using statement (if you don't need the timer elsewhere):
 using (Timer t = new Timer())
{
// Do stuff with t
}
Timer.Dispose() will be called automatically then.

Here's an example of disposing a timer:
    static void TimerTest()
{
using
(Timer t = new Timer(200))
{
t
.Start();
t
.Elapsed += new ElapsedEventHandler(t_Elapsed);
System.Threading.Thread.Sleep(5000); // Do some work here.
}
System.Threading.Thread.Sleep(5000); // Wait before closing the application
}

static void t_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Hello.");
}
Call it in your Main() method. It will print "Hello." for 5 seconds, then the timer is disposed and t_Elapsed won't be called again. After 5 more seconds the app exits.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.