Alex Rivera | Logout

Delayed Dispatch Invoke?

Asked 2010-09-18T08:00:07.040
19

In WPF due to the intricacies on how the interface is updated I sometimes have to perform actions after a short delay.

Currently I'm doing this simply by:

        var dt = new DispatcherTimer(DispatcherPriority.Send);
        dt.Tick += (s, e) =>
        {
            dt.Stop();
            //DoStuff
        };
        dt.Interval = TimeSpan.FromMilliseconds(200);
        dt.Start();

But it's both a bit ugly and perhaps too much overhead to create a new timer each time (?) What's the best from a performance standpoint to do it, ie execute most promptly? And what's good way to rewrite the above code into something like:

        this.Dispatcher.BeginInvoke(new Action(delegate()
        {
            //DoStuff
        }), DispatcherPriority.Send,TimeSpan.FromMilliseconds(200));

Where Timespan is the delay, Thanks for any input :)

Edit
Report

1 Answer

0

You might be able to use the Paul Stowell's DelayBinding class from here: http://www.paulstovell.com/wpf-delaybinding.

With this can can bind to a DependencyProperty on a class that can execute the action when the property changes. The binding will manage the delay. Might be particularly nice if you have a MVVM-type design.

answered 2010-09-18T21:24:18.203

Your Answer