我编写了一个函数,它将使用相对较新的async/await模式在线程上运行任何函数,并稍后通过可选的回调方法通知调用者.该功能非常简单,如下所示:
private async void DoOperationAsync<T>(Func<T> operation, Action<T> resultAction = null)
{
if (operation == null)
throw new ArgumentNullException("operation");
var result = await Task<T>.Factory.StartNew(operation);
// Notify someone that this finished
if (resultAction != null)
resultAction(result);
}
Run Code Online (Sandbox Code Playgroud)
这对于返回值的函数非常有效.它不适用于返回void的函数(或者我可能不够聪明,无法使其工作).我可以编写一个不承担返回类型的特殊情况.但是,我想知道一些C#泛型专家是否可以在这种情况下指出一种处理void的方法.是否有工作方式不涉及无效功能的特殊情况?