Dispatcher.CurrentDispatcher.BeginInvoke does not call

I have a FileSystemWatcher and events related to this when the observed changes to the file are raised in another thread from the user interface thread. To avoid and cross with acce volase fun, I'm trying 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 RaisePathChanged starts as it should. However, the code inside the delegate Action(() => { /*Here*/ }) never called / called, the code is simply skipped.

Why is the code missing, how can I fix it, and is this the best way to ensure that the code runs in the thread that created it in WPF?

Thank you for your time.

+7
c # wpf dispatcher
source share
2 answers

You mix things.

Dispatcher.CurrentDispatcher does not match Application.Current.Dispatcher .

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

Take a look at this.

Dispatcher.CurrentDispatcher vs. Application.Current.Dispatcher

Try using the application manager.

+15
source share

Dispatcher.CurrentDispatcher is the dispatcher of the "current" thread - in this case, the RaisePathChanged thread is RaisePathChanged .
When you say Dispatcher.CurrentDispatcher .NET will create a new dispatcher if there is none.
However, it will not Run() said the dispatcher!
Therefore, when you plan something on it (with BeginInvoke ), it will not actually be executed unless this dispatcher is running.
This is probably the answer to your first question (why doesn't it call?)
To avoid breaking access to end-to-end streams, you need a stream manager that created everything that you are trying to protect, and make sure that it works as a dispatcher.
If something that you are trying to protect was created in the default GUI thread, then use Application.Current.Dispatcher , as the previous answer says, otherwise you will need to clarify a bit and post a little more code before we can respond to your second question. http://www.diranieh.com/NET_WPF/Threading.htm has a rather short introduction to the topic.

+1
source share

All Articles