如何使用异步方法提高我的课程?将异步方法添加到C#中的类?
例如,WCF服务支持将Async方法添加到它们生成的类中,并且在UI中我只需调用一个完整的事件即可。
我尝试搜索细节,但找不到任何关于此的信息。
任何人都可以提供一些示例代码吗?
非常感谢您
如何使用异步方法提高我的课程?将异步方法添加到C#中的类?
例如,WCF服务支持将Async方法添加到它们生成的类中,并且在UI中我只需调用一个完整的事件即可。
我尝试搜索细节,但找不到任何关于此的信息。
任何人都可以提供一些示例代码吗?
非常感谢您
Asynchronous Programming Overview
Asynchronous Programming Design Patterns
,它使用的IAsyncResult设计模式 实现为一个名为BeginOperationName两种方法和 EndOperationName开始和结束异步的异步操作分别操作 OperationName。例如,FileStream类提供 BeginRead和EndRead方法来异步读取 文件中的字节。这些方法实现了Read 方法的异步版本。
这里有一个超快速的不是非常强大的例子我扔在一起是为了什么,我之前用的模板:
public interface IResponse
{
string ResponseCode { get; }
}
public sealed class Response : IResponse
{
private readonly string responseCode;
private Response(string responseCode)
{
this.responseCode = responseCode;
}
public string ResponseCode { get { return this.responseCode; } }
public static IResponse Create(string responseCode)
{
return new Response(responseCode);
}
}
public sealed class DoItCompletedEventArgs : AsyncCompletedEventArgs
{
private readonly IResponse response;
public DoItCompletedEventArgs(
Exception error,
bool canceled,
object userState,
IResponse response) : base(error, canceled, userState)
{
this.response = response;
}
public IResponse Response { get { return this.response; } }
}
public interface IDoStuff
{
event EventHandler<DoItCompletedEventArgs> DoItCompleted;
bool CanProcessAsynchronously { get; }
IResponse DoIt(string[] args);
void DoItAsync(string[] args);
}
public sealed class DoStuff : IDoStuff
{
private delegate IResponse DoItDelegate(string[] args);
public event EventHandler<DoItCompletedEventArgs> DoItCompleted;
public bool CanProcessAsynchronously { get { return true; } }
private DoStuff()
{
}
public static IDoStuff Create()
{
return new DoStuff();
}
public IResponse DoIt(string[] args)
{
return Response.Create(args.Aggregate(string.Empty, (current, arg) => current + arg));
}
public void DoItAsync(string[] args)
{
DoItDelegate doIt = this.DoIt;
doIt.BeginInvoke(args, this.DoDoItCompleted, doIt);
}
private void DoDoItCompleted(IAsyncResult result)
{
if (result == null)
{
return;
}
var doIt = result.AsyncState as DoItDelegate;
if (doIt == null)
{
return;
}
var response = doIt.EndInvoke(result);
var doItCompleted = this.DoItCompleted;
if (doItCompleted != null)
{
doItCompleted(this, new DoItCompletedEventArgs(null, false, null, response));
}
}
}
internal static class Program
{
private static void Main()
{
var doStuff = DoStuff.Create();
if (doStuff.CanProcessAsynchronously)
{
var response = doStuff.DoIt(new[] { "stuff 1 ", "more stuff 1" });
Console.WriteLine(response.ResponseCode);
}
else
{
doStuff.DoItCompleted += DoItCompleted;
doStuff.DoItAsync(new[] { "stuff 2 ", "more stuff 2" });
}
Console.ReadLine();
}
private static void DoItCompleted(object sender, DoItCompletedEventArgs e)
{
Console.WriteLine(e.Response.ResponseCode);
}
}
这是一个相当开放式的问题。你可能应该更具体。 SO真的不是为了避免你阅读文档。无论如何,一般来说,WPF和WinForm程序会从异步/等待关键字中受益匪浅,因为这些程序通常需要在主UI线程上执行任何操作 - async/await会极大地简化您的任务,因为它们默认在UI上执行延续线程,使您无需一直将Dispatcher.BeginInvoke中的继续代码封装起来。我不确定WCF是否会受益匪浅 –