Peter
Peter

Reputation: 3184

Silverlight run DispatchTimer only once

I'm trying to figure out a way in Silverlight / C# to make a DispatchTimer run only once.

I have a user form and when submitted I want to display a message for 10 seconds and then disappear and kill the DispatchTimer thread.

I know how to make a DispatchTimer that repeats:

clock.Interval = TimeSpan.FromSeconds(10);
clock.Tick += clockTick;

clock.Start();

But I want that thread to end as soon as it completes.

Upvotes: 0

Views: 685

Answers (2)

K Mehta
K Mehta

Reputation: 10533

This should work for you:

DispatcherTimer clock = new DispatcherTimer();
clock.Interval = TimeSpan.FromSeconds(10);
clock.Tick += (object sender, EventArgs e) =>
{
    clock.Stop();
    // Some code here
};
clock.Start();

An anonymous event handler will also keep things "in the same place" in case you don't want to widen the scope of your DispatcherTimer object.

Upvotes: 5

Rafael Dowling Goodman
Rafael Dowling Goodman

Reputation: 913

Stop the timer in your clockTick handler once it fires.

Upvotes: 1

Related Questions