0

我正在尝试使用Singleton Pattern构建简单的类。

我想要完成的是在我的主要类 2 函数中负责处理异步方法调用的结果。一会处理正确的结果,二会处理所有错误。

执行所有异步操作的类如下所示:

class EWS : SingletonBase<EWS>
{
    private EWS()
    {
        //private constructor
    }


    private int LongRunningMethod(Action<string> error)
    {
        int x = 0;
        for (int i = 0; i < 10; i++)
        {
            //Console.WriteLine(i);
            x += i;
            Thread.Sleep(1000);
        }
        //here I can do try...catch and then call error("Description")
        return x;
    }

    public class CommandAndCallback<TSuccess, TError>
    {
        public TSuccess Success { get; set; }
        public TError Error { get; set; }
    }

    public void DoOperation(Action<int> success, Action<string> error)
    {
        Func<Action<string>, int> dlgt = LongRunningMethod;
        //Func<Action<string>, int> dlgt = new Func<Action<string>, int>(LongRunningMethod);

        CommandAndCallback<Action<int>, Action<string>> callbacks = new CommandAndCallback<Action<int>, Action<string>>() { Success = success, Error = error };

        IAsyncResult ar = dlgt.BeginInvoke(error,MyAsyncCallback, callbacks); 
    }

    public void MyAsyncCallback(IAsyncResult ar)
    {
        //how to access success and error here???
        int s ;
        Func<Action<string>, int> dlgt = (Func<Action<string>,int>)ar.AsyncState;
        s = dlgt.EndInvoke(ar);
        //here I need to call success or error that were passed to DoOperation
    }
}

在我的主课中,我想这样调用我的方法:

private void Operation_OK(int count)
{
     //here handle OK
}

private void Operation_ERROR(string error)
{
     //here handle ERROR
}

private void button1_Click(object sender, EventArgs e)
{
    EWS.Instance.DoOperation(Operation_OK, Operation_ERROR);
}

我应该如何修改我的 EWS 类,以便我可以像上面所示那样调用它。
如何使用 lambda 表达式代替委托?
调用这样的方法是个好主意吗?我需要在我的班级中有 2 或 3 个方法,并且能够以所有形式独立地调用它们。

4

2 回答 2

1

代替

private void MyAsyncCallback(IAsyncResult ar)
{
    int s;
    MethodDelegate dlgt = (MethodDelegate)ar.AsyncState;
    s = dlgt.EndInvoke(ar);
    MessageBox.Show(s.ToString());
}

private void MyAsyncCallback(IAsyncResult ar)
{
    int s;
    MethodDelegate dlgt = (MethodDelegate)((AsyncResult)ar).AsyncDelegate;
    s = dlgt.EndInvoke(ar);
    MessageBox.Show(s.ToString());
}

更新的答案

IAsyncResult ar = dlgt.BeginInvoke(错误,MyAsyncCallback,回调,错误);

现在在 MyAsyncCallback

string error = (string) ar.AsyncState;
if(error!=null)
    return;
于 2012-10-03T09:25:34.987 回答
0

得到这个工作:

    private int LongRunningMethod(Action<string> error)
    {
        int x = 0;
        try
        {
            x = 20 / x;
        }
        catch (Exception ex)
        {
            error("Bad error!");
        }
        return x;
    }

    public class CommandAndCallback<TCallback, TError>
    {
        public TCallback Callback { get; set; }
        public TError Error { get; set; }
        public Func<Action<string>, int> Delegate { get; set; }
    }

    public void DoOperation(Action<int> callback, Action<string> error)
    {
        Func<Action<string>, int> dlgt = LongRunningMethod;
        CommandAndCallback<Action<int>, Action<string>> callbacks = new CommandAndCallback<Action<int>, Action<string>>() { Callback = callback, Error = error, Delegate=dlgt };
        IAsyncResult ar = dlgt.BeginInvoke(error,MyAsyncCallback, callbacks); 
    }

    private void MyAsyncCallback(IAsyncResult ar)
    {
        int s ;
        CommandAndCallback<Action<int>, Action<string>> callbacks= (CommandAndCallback<Action<int>, Action<string>>)ar.AsyncState;
        s = callbacks.Delegate.EndInvoke(ar);
        callbacks.Success(s);
    }

这工作得很好。
只有一个问题,如果LongRunningMethod我调用错误方法MyAsyncCallback也被调用。

所以这只是要修复的一件事,所以当调用错误方法MyAsyncCallback时不会被调用。

于 2012-10-03T10:25:44.047 回答