Reputation: 37633
I have got this code in WPF application.
public void NotifyEntityUpdated(int userId, int entityId)
{
Action currentAction = () =>
{
EntityUpdatedByUser(userId, entityId);
SendEmail(userId, entityId);
};
this.Dispatcher.BeginInvoke(currentAction);
}
How I can execute it asynchronously in .net 4?
As I see I cannot use async/await like this...
public async Task<T> DoSomethingAsync<T>(Func<T, Task> resultBody) where T : Result, new()
{
T result = new T();
await resultBody(result);
return result;
}
Any clue?
Upvotes: 1
Views: 836
Reputation: 37633
Finally I found the following:
So I found a working solution.
public void NotifyEntityUpdated(int userId, int entityId)
{
Action currentAction = () =>
{
EntityUpdatedByUser(userId, entityId);
};
this.Dispatcher.Invoke(currentAction, DispatcherPriority.Send);
Action currentAction2 = () =>
{
SendEmail(userId, entityId);
};
this.Dispatcher.BeginInvoke(currentAction2, DispatcherPriority.Loaded);
}
Upvotes: 0
Reputation: 709
Using the .NET Task's you can do something like this.
1- First resolve and run the task
private Task ExecuteTask(Result result)
{
return Task.Run(() =>
{
this.resultBody(result)
});
}
2- Call it like this
await this.ExecuteTask(result);
// I dont have the VS here but I hope it will work ,good luck!
Upvotes: 3