Search code examples
c#asynchronousprismasync-awaitprism-4

RaiseCanExecuteChanged called while await causes deadlock


I am using WPF and DelegateCommand from PRISM and have the following problem:

I start an async operation like:

public async void ProgramDevice()
{
    var result = await FirmwareLoader.DownloadFirmwareAsync();
}

Inside this method an event is fired which I registered to and should update my DelegateCommand so it can't be executed:

//UiCommand is of type DelegateCommand
Engine.IsProgrammedChanged += 
    (s, e) => Dispatcher.Invoke(() => UiCommand.RaiseCanExecuteChanged());

Now I have the problem, that the RaiseCanExecuteChanged causes a deadlock (I checked and the Dispatcher.Invoke does not cause it, because when I e.g. show a MessageBox instead it works fine).

Am I doing something wrong or how can I work around this problem?


Solution

  • Found the problem:
    It was not the RaiseCanExecuteChanged, but the actual CanExecute which is triggered by it. In there I had an AsyncLock which waited for the programming task to be finished, before returning the value I use to descide if UiCommand can be executed --> deadlock as the programming task triggered it...

    I solved it by simple using the "sync" property (which does not use the lock and just returns the current value/stat) of the value I need.