2011-08-15 24 views
0

如何使用異步方法提高我的課程?將異步方法添加到C#中的類?

例如,WCF服務支持將Async方法添加到它們生成的類中,並且在UI中我只需調用一個完整的事件即可。

我嘗試搜索細節,但找不到任何關於此的信息。

任何人都可以提供一些示例代碼嗎?

非常感謝您

+0

這是一個相當開放式的問題。你可能應該更具體。 SO真的不是爲了避免你閱讀文檔。無論如何,一般來說,WPF和WinForm程序會從異步/等待關鍵字中受益匪淺,因爲這些程序通常需要在主UI線程上執行任何操作 - async/await會極大地簡化您的任務,因爲它們默認在UI上執行延續線程,使您無需一直將Dispatcher.BeginInvoke中的繼續代碼封裝起來。我不確定WCF是否會受益匪淺 –

回答

4

Asynchronous Programming Overview

Asynchronous Programming Design Patterns

,它使用的IAsyncResult設計模式 實現爲一個名爲BeginOperationName兩種方法和 EndOperationName開始和結束異步的異步操作分別操作 OperationName。例如,FileStream類提供 BeginRead和EndRead方法來異步讀取 文件中的字節。這些方法實現了Read 方法的異步版本。

1

這裏有一個超快速的不是非常強大的例子我扔在一起是爲了什麼,我之前用的模板:

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); 
    } 
}