I have a similar problem than here : WPF MVVM Light: Command.RaiseCanExecuteChanged() doesn't work, using commands with WPF and have my GUI not working until I click somewhere in the scren. I don't use MVVM Light.
I call an external DLL to do some action, by calling ExternalDLL.Start(), and call GetStatus() to know if the action started. If I get the correct status in return, I change the actual action, and it have to activate a button on my GUI.
The button don't activate himself until I click somewhere.
I checked for the thread, but it seems it's on the same thread, I tried to put it in the GUI thread to, by using Application.Current.Dispatcher.BeginInvoke
, but it didn't work too.
Here is my code :
private async void StartScanCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
ExternalDLL.Start();
WaitForStarting();
}
private async void WaitForStarting()
{
Waiting();
Stopwatch chrono = new Stopwatch();
chrono.Start();
bool started = false;
while (chrono.ElapsedMilliseconds < 20000)
{
if (ExternalDLL.GetStatus() != ExternalDLL.Status.Started)
{
await Task.Delay(100);
}
else
{
started = true;
chrono.Stop();
StartedAction();
break;
}
}
if (!started)
{
MessageBox.Show("Error");
}
}
The Waiting()
method call activate a button in the GUI and work. but the StartedAction()
have to activate a button too, and doesn't work.
Here is the code for started action :
private void StartedAction()
{
_actualAction = ActualAction.DoingAction;
}
And here is the button's can execute method :
private void SomeButtonCommand_CanExecute(object sender,
CanExecuteRoutedEventArgs e)
{
e.CanExecute = _actualAction == ActualAction.DoingAction;
}
What am I doing wrong ?