✅ Help needed: Buttons update enabled state after two clicks.
Hello,
I need help to find out why it takes two clicks before the buttons change the enabled state.
Please see the attached video and also my code below:
4 Replies
RefreshCanExecutes();
doesn't wait for the service to actually start. You queue a bit of work onto the thread pool to start the worker at some point in the future, then immediately call RefreshCanExecutes();
, which calls _workerService.IsRunning
. But the worker isn't running yet
You shouldn't be discarding tasks. The fact that you had to throw a Task.Run
into the mix is presumably to work around the fact that the compiler was moaning at you for now awaiting the _workerService.StartAsync
call? That was your clue 😉
Also, if you use as
, always check for null
. If you don't expect the cast to fail, use a normal cast, not as
. The reason is that if you use as
but the cast does fail unexpectedly, you'll get an unhelpful NullReferenceException
, rather than a helpful InvalidCastException
.
So:
@canton7 thank you for your responses, they helped.
Great! Has that solved your problem?
Yes it has.