1

I'm currently developping an application in MVVM. I'm using for all buttons RelayCommand to perform action. The fact is that, with RelayCommand, the button is disabled when clicked on it, the time that the command is executing. Because our rendering engine is a little bit heavy, when we open a new window, the button stay disabled for a bit, like a second.

My question is : is there a way to disable this behaviour and let the button enabled, because it changes style ? Changing disabled style is not an option ...

I haven't see that kind of behavior on the net or in the documentation ?

Thank you

EDIT :

Two differents implementations of my RelayCommand

        AnswerCommand = new RelayCommand(p => AnswerMail(p), p => IsMailSelected());
        DeleteMailCommand = new RelayCommand(p => Task.Run(() => DeleteMail()), p => IsMailSelected());
cdie
  • 4,014
  • 4
  • 34
  • 57

1 Answers1

0

I think you should synchronize the access for the block existing in the ICommand instance Execute method and every time you get there, start a new thread. If you want to click on that Button many times, having that lock section there, will put your calls in a queue. If you want to reject the requests received while it's still working you can implement the balking pattern.

https://en.wikipedia.org/wiki/Balking_pattern

This way, your button will stop being disabled when the created thread is starting.

I am not sure if this approach helps you but i think it's a feasible scenario.

One more thing, i've just found this:

Patterns for Asynchronous MVVM Applications: Commands

i haven't used it before but looks nice, i will give it a try tonight. Good luck!

Olaru Mircea
  • 2,570
  • 26
  • 49
  • The main goal of want I want to do is not to apply disabled style when button is clicked, event if it's actually blocking the UI thread; BTW many of my commands are async (such as new RelayCommand(p => Task.Run(() => MysAyncFunc())), but I have a little time where button is disabled – cdie Jun 19 '15 at 10:53
  • @cdie If you send your time consuming work to another thread, your Execute method will return immediately and the Button will return to its enabled status. – Olaru Mircea Jun 19 '15 at 10:55
  • yeah, in fact, I will do everything asynchronously – cdie Jun 19 '15 at 10:57