Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does BackgroundWorker_RunWorkerCompleted not update GUI?
    text
    copied!<p>I already searched the forum but no asked question fits to my problem I think ...</p> <p>I use a BackgroundWorker to handle a very time consuming operation. After this operation finishes a button should be set to enabled so that the user can perform another operation. </p> <p>I'm using WPF and I'm following the MVVM pattern, so I have no direct access to this button. I have to call a method in the BackgroundWorker_RunWorkerCompleted event handler which sets a Property, representing the buttons enabled-state, to true. </p> <p>That all works fine except one thing: The button is only redrawn after I e.g. click into the window (or maximize the window, ...). Thats very annoying and took me the whole day to get rid of this behaviour but I can't find a solution ... </p> <p>The BackgroundWorker_RunWorkerCompleted event handler looks like this:</p> <pre><code> void fileLoadBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { SetButtonToEnabled(); } </code></pre> <p>Has anyone any idea how to fix this issue? </p> <p>Edit:</p> <p>The button is bound to a command: </p> <pre><code>&lt;Button Name="btnLoadTargetFile" Command="{Binding Path=LoadTargetCommand}" .../&gt; </code></pre> <p>This command is a RelayCommand as Smith stated in his blog (<a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/magazine/dd419663.aspx</a>) and looks like this:</p> <pre><code>public RelayCommand LoadTargetCommand { get { if (loadTargetCommand == null) { loadTargetCommand = new RelayCommand(param =&gt; this.OnRequestLoadFile(BusinessLogic.CustomTypes.TreeType.Target), param =&gt; this.CanLoadTarget); } return loadTargetCommand; } set { loadTargetCommand = value; } } </code></pre> <p>this.CanLoadTarget is set to true by the SetButtonToEnabled(); method</p> <p>Edit 2: </p> <p>the following code 'works': </p> <pre><code>fileLoadBackgroundWorker.RunWorkerAsync(argumentList); while(fileLoadBackgroundWorker.IsBusy) System.Windows.Forms.Application.DoEvents(); SetButtonToEnabled(); </code></pre> <p>but that is some sort of really dangerous and ugly code ...</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