开发者

How to use delegate to perform callback between caller and web service helper class?

I have 2 classes A and B, where they belongs to the same namespace but resides in seperate files namely a.cs and b.cs, where class B essentially is a helper wrapping a web service call as follow:

public class A
{
    public A() // constructor
    {
        protected static B b = new B();
    }

    priva开发者_StackOverflowte void processResult1(string result)
    {
        // come here when result is successful
    }

    private void processResult2(string result)
    {
        // come here when result is failed
    }
    static void main()
    {
        b.DoJobHelper(...);
    }
}

public class B
{
    private com.nowhere.somewebservice ws;
    public B()
    {
        this.ws = new com.nowhere.somewebservice();
        ws.JobCompleted += new JobCompletedEventHandler(OnCompleted);
    }
    void OnCompleted(object sender, JobCompletedEventArgs e)
    {
        string s = e.Result;
        Guid taskID = (Guid)e.UserState;
        switch (s)
        {
             case "Success":
             // Call processResult1(); 
             break;
             case "Failed":
             // Call processResult2(); 
             break;
             default: break;
        }
    }
    public void DoJobHelper()
    {
        Object userState = Guid.NewGuid();
        ws.DoJob(..., userState);
    }        
}

(1) I have seen texts on the net on using delegates for callbacks but failed to apply that to my case. All I want to do is to call the appropriate processResult() method upon OnCompleted() event, but dunno how to and where to declare the delegate:

public delegate void CallBack(string s);

(2) There is a sender object passed in to OnCompleted() but never used, did I miss anything there? Or how can I make good use of sender?

Any helps appreciated.


1)

public class A
{
    public A() // constructor
    {
        protected static B b = new B(processResult1, processResult2);
    }

    private void processResult1(string result)
    {
        // come here when result is successful
    }

    private void processResult2(string result)
    {
        // come here when result is failed
    }
    static void main()
    {
        b.DoJobHelper(...);
    }
}

public class B
{
    private com.nowhere.somewebservice ws;
    private Action<string> success;
    private Action<string> failure;
    public B(Action<string> success, Action<string> failure)
    {
        this.success = success;
        this.failure = failure;
        this.ws = new com.nowhere.somewebservice();
        ws.JobCompleted += new JobCompletedEventHandler(OnCompleted);
    }
    void OnCompleted(object sender, JobCompletedEventArgs e)
    {
        string s;
        Guid taskID = (Guid)e.UserState;
        //this switch will never do anything because s is null right now.
        //you'll need to check some actual state, or translate some other
        //state into the "Success" and "Failure" words for the s variable
        //before you call this switch statement.
        switch (s)
        {
             case "Success":
             {    
               success(s);
               break;
             }
             case "Failed":
             {
               failure(s);
               break;
             }
             default: break;
        }
    }
    public void DoJobHelper()
    {
        Object userState = Guid.NewGuid();
        ws.DoJob(..., userState);
    }        
}

2) don't bother using sender. it's only important if you have more than one object's events being handled by the same event handler method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜