0

Singleton Patternを使用して単純なクラスを構築しようとしています。

私が達成しようとしているのは、非同期メソッド呼び出しの結果の処理を担当するメイン クラス 2 関数を使用することです。1 つは正しい結果を処理し、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 クラスをどのように変更すればよいですか。
デリゲートの代わりにラムダ式を使用するには?
このようなメソッドを呼び出すのは良い考えですか? クラスに 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);
    }

これはうまくいきます。エラーメソッドも呼び出された
場合、問題は1つだけです。LongRunningMethodMyAsyncCallback

したがって、これは修正する 1 つのみであるため、エラー メソッドが呼び出されたときに呼び出されMyAsyncCallbackません。

于 2012-10-03T10:25:44.047 に答える