Is it safe to do something like this:
private void MyFunction()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += (object sender, object e) =>
{
timer.Stop();
// Some code here
};
timer.Start();
}
Matt raise the point that the way you attach the anonymous method that there is no easy way to detach it. Here is a general pattern you can use to enable you to detach if necessary.
However in this specific case there is nothing wrong with the way your original code works since the timer becomes collectable as soon as it is stopped.