Code first. This is what I'm trying to do. I'm close, but I think I just need to fix the way I've defined my parameter in the UpdateButton method.
private async void UpdateButton(Action<bool> post) { if (!await post()) ErrorBox.Text = "Error posting message."; } private void PostToTwitter() { UpdateButton(async () => await new TwitterAction().Post("Hello, world!")); } private void PostToFacebook() { UpdateButton(async () => await new FacebookAction().Post("Hello, world!")); }
Unfortunately, the !await post()
doesn't work because, "Type 'void' is not awaitable." So the question is, how do I define my parameter in this method to support an awaitable parameter?
Here's how the TwitterAction().Post() is defined...
public virtual async Task<bool> Post(string messageId){...}
An async function (or AyncFunction) in the context of Async is an asynchronous function with a variable number of parameters where the final parameter is a callback.
An async function can have more than one await expression in its scope which can pause the execution inside its function scope. Once the passed Promise has been resolved, it will resume the async function's execution.
The main difference between an async function expression and an async function statement is the function name, which can be omitted in async function expressions to create anonymous functions.
private async void UpdateButton(Func<Task<bool>> post) { if (!await post()) ErrorBox.Text = "Error posting message."; }
--EDIT--
UpdateButton(()=>Post("ss")); private async void UpdateButton(Func<Task<bool>> post) { if (!await post()) this.Text = "Error posting message."; } public virtual async Task<bool> Post(string messageId) { return await Task.Factory.StartNew(() => true); }
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