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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With