0

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}"/>
Sys
  • 443
  • 1
  • 8
  • 28

2 Answers2

2

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.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • I've tried this by adding it as the last line in ExecuteThread Method, however it didn't help. – Sys Jul 29 '11 at 13:30
  • 2
    @Sys, try to call this method on the UI thread (using Dispatcher.Current.Invoke) – Thomas Levesque Jul 29 '11 at 13:39
  • I've tried Application.Current.Dispatcher.Invoke(new Action(delegate { IsExecuting = true; })); but again not working... – Sys Jul 29 '11 at 13:56
  • You can assign the property on any thread, it doesn't matter. What I told you to try is to call CommandManager.InvalidateRequerySuggested() on the UI thraed – Thomas Levesque Jul 29 '11 at 14:22
0

I think you should post some code to better understand your problem.

I'm quite sure it is a thread issue as you can't change the status of a control's form from a different Thread.

I think all you need to to is use a Delegate to update the status of your button from other thread

How to update the GUI from another thread in C#?

Community
  • 1
  • 1
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70