C# 动作T同步和异步

C# 动作T同步和异步,c#,asynchronous,C#,Asynchronous,我有一个ContextMenuStrip控件,允许您执行两种不同的操作:Sync和Async 我试图使用泛型涵盖所有内容,所以我做到了: public class BaseContextMenu<T> : IContextMenu { private T executor; public void Exec(Action<T> action) { action.Invoke(this.executor); } public v

我有一个ContextMenuStrip控件,允许您执行两种不同的操作:
Sync
Async

我试图使用泛型涵盖所有内容,所以我做到了:

public class BaseContextMenu<T> : IContextMenu
{
   private T executor;

   public void Exec(Action<T> action)
   {
      action.Invoke(this.executor);
   }

   public void ExecAsync(Action<T> asyncAction)
   {
       // ...
   }

下面是Jeffrey Richter关于.NET异步编程模型的文章

以下是如何使用BeginInvoke的示例:

public class BaseContextMenu<T> : IContextMenu
{
    private T executor;

    public void Exec(Action<T> action)
    {
        action.Invoke(this.executor);
    }

    public void ExecAsync(Action<T> asyncAction, AsyncCallback callback)
    {
        asyncAction.BeginInvoke(this.executor, callback, asyncAction);
    }
}
公共类BaseContextMenu:IContextMenu
{
私人信托执行人;
公开作废执行官(行动)
{
action.Invoke(这个执行者);
}
public void ExecAsync(操作异步操作,异步回调)
{
asyncAction.BeginInvoke(this.executor,callback,asyncAction);
}
}
下面是一个可以传递给ExecAsync的回调方法:

private void Callback(IAsyncResult asyncResult)
{
    Action<T> asyncAction = (Action<T>) asyncResult.AsyncState;
    asyncAction.EndInvoke(asyncResult);
}
私有无效回调(IAsyncResult asyncResult)
{
Action asyncAction=(Action)asyncResult.AsyncState;
EndInvoke(asyncResult);
}
最简单的选项:

// need this for the AsyncResult class below
using System.Runtime.Remoting.Messaging;

public class BaseContextMenu<T> : IContextMenu
{
    private T executor;

    public void Exec(Action<T> action) {
        action.Invoke(this.executor);
    }

    public void ExecAsync(Action<T> asyncAction) {
        // specify what method to call when asyncAction completes
        asyncAction.BeginInvoke(this.executor, ExecAsyncCallback, null);
    }

    // this method gets called at the end of the asynchronous action
    private void ExecAsyncCallback(IAsyncResult result) {
        var asyncResult = result as AsyncResult;
        if (asyncResult != null) {
            var d = asyncResult.AsyncDelegate as Action<T>;
            if (d != null)
                // all calls to BeginInvoke must be matched with calls to
                // EndInvoke according to the MSDN documentation
                d.EndInvoke(result);
        }
    }
}
//下面的AsyncResult类需要这个
使用System.Runtime.Remoting.Messaging;
公共类BaseContextMenu:IContextMenu
{
私人信托执行人;
公开作废执行官(行动){
action.Invoke(这个执行者);
}
public void ExecAsync(操作异步操作){
//指定asyncAction完成时要调用的方法
asyncAction.BeginInvoke(this.executor,ExecAsyncCallback,null);
}
//此方法在异步操作结束时被调用
私有void ExecAsyncCallback(IAsyncResult结果){
var asyncResult=结果为asyncResult;
if(asyncResult!=null){
var d=asyncResult.AsyncDelegate作为操作;
如果(d!=null)
//对BeginInvoke的所有调用必须与对的调用匹配
//根据MSDN文档进行EndInvoke
d、 EndInvoke(结果);
}
}
}

谢谢,这就是我要找的。我对lambda表达式有一个问题,我不需要多线程编程课程+杰夫文章的参考文献为1。这真的很有见地,帮了我很大的忙。我不认为那个链接到以前的地方了
// need this for the AsyncResult class below
using System.Runtime.Remoting.Messaging;

public class BaseContextMenu<T> : IContextMenu
{
    private T executor;

    public void Exec(Action<T> action) {
        action.Invoke(this.executor);
    }

    public void ExecAsync(Action<T> asyncAction) {
        // specify what method to call when asyncAction completes
        asyncAction.BeginInvoke(this.executor, ExecAsyncCallback, null);
    }

    // this method gets called at the end of the asynchronous action
    private void ExecAsyncCallback(IAsyncResult result) {
        var asyncResult = result as AsyncResult;
        if (asyncResult != null) {
            var d = asyncResult.AsyncDelegate as Action<T>;
            if (d != null)
                // all calls to BeginInvoke must be matched with calls to
                // EndInvoke according to the MSDN documentation
                d.EndInvoke(result);
        }
    }
}