Search code examples
wpfthreadpoolcanexecute

WPF CanExecute and ThreadPool


My application executes a webservice call and when it happens, the IsExecuting status is set to true and the execute button is disabled. Because the application is not responsive during this period i change the process so the execution is happening in a seperate thread. However the problem that i'm noticing now is, the execute button is still disabled and only when i clicked on the Interface the button gets enabled. How can i fix this?

EDIT: In Codebehind:

private void Execute()
{
    ThreadPool.QueueUserWorkItem(new WaitCallback(ExecuteThread));
}

private bool CanExecute
{
    get  { return !IsExecuting; }
}

private void ExecuteThread(Object o)
{
    IsExecuting = true;

    // Process execution here ...

    IsExecuting = false;
}


In Xaml:

<Button Content="Execute" Command="{Binding Path=ExecCommand}"/>

Solution

  • So you need to refresh the CanExecute status of your commands? Just call CommandManager.InvalidateRequerySuggested(), this will cause CanExecute to be reevaluated on bound commands.