Home Async implementation of ICommand reasonable?
Reply: 0

Async implementation of ICommand reasonable?

user1294
1#
user1294 Published in July 18, 2018, 4:57 am

I've got an async implementation of the ICommand interface that of course has to implement the method void Execute(object parameter)

The actual implementation is then async like the following:

public async void Execute(object parameter)
{
    await ExecuteAsync((T)parameter);
}

The ExecuteAsync method is then defined as follows:

private readonly Func<T, Task> execute;
private bool isExecuting;

public async Task ExecuteAsync(T parameter)
{
    try
    {
        isExecuting = true;
        InvokeCanExecuteChanged();
        await execute(parameter);
    }
    finally
    {
        isExecuting = false;
        InvokeCanExecuteChanged();
    }
}

Now I do know that except for EventHandlers void should be avoided as return type for async methods. However ICommand is more or less a wrapper to clearly separate the view model from the actual event handlers. So am I fine with this implementation or will I run into problems?

Especially I'd like to know if I can safely use the execute command and rely on the Task to finish before the handler or will the handler complete regardless of the Task's status?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.268757 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO