MoonKnight MoonKnight - 1 year ago 291
C# Question

Dispatcher.CurrentDispatcher.BeginInvoke Not Invoking

I have a

and the events raised by this when a watched file changes are raised on a different thread from the UI thread. To avoid and cross-thread acess volation fun, I am attempting to use

public void RaisePathChanged(object sender, RenamedEventArgs e)
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
// Some code to handle the file state change here.

This compiles fine and the
is fired as it should be. However, the code inside the delegate
Action(() => { /*Here*/ })
never gets called/invoked, the code is merely skipped.

Why is the code being skipped, how can I fix it and is this the best way to insure code is run on the thread that created it in WPF?

Thanks for your time.

Answer Source

You are mixing up things.

Dispatcher.CurrentDispatcher is not the same as Application.Current.Dispatcher.

The second one is the one you seem to be looking for.

Take a look at this.

Dispatcher.CurrentDispatcher vs. Application.Current.Dispatcher

Try it out with application dispatcher.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download