Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Dispatcher.Invoke 'hanging'
    text
    copied!<p>I have a somewhat complex WPF application which seems to be 'hanging' or getting stuck in a Wait call when trying to use the dispatcher to invoke a call on the UI thread.</p> <p>The general process is:</p> <ol> <li>Handle the click event on a button</li> <li>Create a new thread (STA) which: creates a new instance of the presenter and UI, then calls the method <strong>Disconnect</strong></li> <li>Disconnect then sets a property on the UI called <strong>Name</strong></li> <li>The setter for Name then uses the following code to set the property:</li> </ol> <pre><code> if(this.Dispatcher.Thread != Thread.CurrentThread) { this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate{ this.Name = value; // Call same setter, but on the UI thread }); return; } SetValue(nameProperty, value); // I have also tried a member variable and setting the textbox.text property directly. </code></pre> <p>My problem is that when the dispatcher <strong>invoke</strong> method is called it seems to hang every single time, and the callstack indicates that its in a sleep, wait or join within the Invoke implementation.</p> <p>So, is there something I am doing wrong which I am missing, obvious or not, or is there a better way of calling across to the UI thread to set this property (and others)?</p> <p><strong>Edit:</strong> The solution was to call System.Windows.Threading.Dispatcher.Run() at the end of the thread delegate (e.g. where the work was being performed) - Thanks to all who helped.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload