How to use delegate to perform callback between caller and web service helper class?
- by codemonkie
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();
}
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;
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.