非同期メソッドでクラスを改善するにはどうすればよいですか?C#のクラスに非同期メソッドを追加しますか?
例:WCFサービスは、生成するクラスに非同期メソッドを追加し、UIで完全なイベントを呼び出すだけで済みます。
詳細を検索しようとしましたが、これに関する情報が見つかりません。
少しサンプルコードを提供できますか?
ありがとうございました非同期メソッドでクラスを改善するにはどうすればよいですか?C#のクラスに非同期メソッドを追加しますか?
例:WCFサービスは、生成するクラスに非同期メソッドを追加し、UIで完全なイベントを呼び出すだけで済みます。
詳細を検索しようとしましたが、これに関する情報が見つかりません。
少しサンプルコードを提供できますか?
ありがとうございましたAsynchronous Programming Overview
Asynchronous Programming Design Patterns
たIAsyncResultのデザインパターンが が始まると、非同期を終了BeginOperationNameという名前の2つの方法と EndOperationNameとして実装されて使用しています非同期操作オペレーション オペレーション名。たとえば、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);
}
}
これは非常にオープンエンドの問題です。あなたはおそらくより具体的でなければなりません。あなたが文書を読むことからあなたを救うために、本当にここにはありません。いずれにせよ、一般的に、WPFとWinFormプログラムは、主にUIのスレッド上で動作する必要があるため、async/awaitキーワードの効果が大きくなります。async/awaitはUIで継続を実行するためにタスクを大幅に簡素化しますあなたは継続コードを常にDispatcher.BeginInvokeにラップする必要がなくなります。 WCFがそれほど利益を得るかどうかはわかりません... –