Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ICommand.CanExecute async

I have a Command, which Check for CanExecute takes much of time. Now, I ask me, if it is possible to run the Method for CanExecute asynchronous?

like image 365
BennoDual Avatar asked Jul 17 '26 12:07

BennoDual


2 Answers

Well it is possible. You should use the CanExecutedChanged. Do your long lasting check in the background(you could use BackgroundWorker), store the result if you can execute or not, fire the event and just return the cached value in your CanExecute.

like image 65
dowhilefor Avatar answered Jul 19 '26 02:07

dowhilefor


No, you cannot directly run it asynchronously. Nor should you, you do not know when the binding subsystem is going to call it.

There is nothing stopping you from starting a background thread from within that function, but to be honest that would make very little sense. If your CanExecute code is taking that long to execute then you really need to re-evaluate what you are doing, whether that means redo the code, or redo the UI to remove the dependency on the CanExecute.

If you use the DelegateCommand<T> from Prism you can force anything bound to the command to re-evaluate the CanExecute when you choose. This can be done by calling the RaiseCanExecuteChanged() function on the command. If you then have a background thread running which calls this when necessary it should function in the manner you want.

like image 43
slugster Avatar answered Jul 19 '26 01:07

slugster