C# 另一个类中的DownloadStringCompletedEventHandler回调

C# 另一个类中的DownloadStringCompletedEventHandler回调,c#,callback,C#,Callback,我有两节课,下面是在我继续提问之前发生的事情的分类 myClass1: myClass1(){ myClass2 c2 = new myClass2(); c2.service(); } public void myFunction1(){ Console.Write("Function returned!"); } public void service(){ callWebService(myFunction1); // The paramete

我有两节课,下面是在我继续提问之前发生的事情的分类

myClass1:

myClass1(){

    myClass2 c2 = new myClass2();
    c2.service();

}

public void myFunction1(){

    Console.Write("Function returned!");

}
public void service(){

    callWebService(myFunction1); // The parameter you pass here is the function that control 
                      // will pass back to once the called function is done.

}

public void callWebService(DownloadStringCompletedEventHandler callback){

    //Calls web service and does some other operations

}
myClass2:

myClass1(){

    myClass2 c2 = new myClass2();
    c2.service();

}

public void myFunction1(){

    Console.Write("Function returned!");

}
public void service(){

    callWebService(myFunction1); // The parameter you pass here is the function that control 
                      // will pass back to once the called function is done.

}

public void callWebService(DownloadStringCompletedEventHandler callback){

    //Calls web service and does some other operations

}
最后是问题。正如您在上面看到的,我有两个类,class1调用class2中的函数。该函数调用类2中另一个调用Web服务的函数。一旦web服务完成,控制流就会传回函数调用中传递的任何函数

但这意味着您只能使用一个类,因为回调函数应该在同一个类中。所以问题是,我如何在另一个类中传递一个函数作为回调函数


希望所有这些都是有意义的,请不要犹豫问任何事情来澄清一点。谢谢

您可以修改
服务
类并将
MyClass1的
方法传递给它。例如,在下面的代码中,函数
ServiceCallComplete
作为参数传递给
服务
类构造函数

函数可以另存为或委托类型(取决于回调函数定义)。服务作业完成后,调用委托(
\u callback()
)将在
MyClass1
上调用回调函数

public class MyClass1
{
    //The callback Function
    public void ServiceCallComplete()
    {
        Console.WriteLine("Function returned.");
    }

}

public class Service
{
    //delegate to store the callback function.
    private readonly Action _callBack;

    public Service(Action callBack)
    {
        //store the callback function
        _callBack = callBack;
    }

    public void Method()
    {
        //long running operation
        .
        .
       //Invoke the callback
        _callBack();
    }
}


MyClass1 obj = new MyClass1();
Service svc = new Service(obj.ServiceCallComplete);
svc.Method();

不要传递委托,而是使用事件:

class MyClass1
{
    public MyClass1()
    {
        var c2 = new MyClass2();

        c2.ActionwebServiceCalled += MyCallBack; //register for the event
        c2.CallWebService();
    }

    public void MyCallBack(object sender, DownloadStringCompletedEventArgs e)
    {
        Console.Write("Function returned!");
    }
}

class MyClass2
{
    public event DownloadStringCompletedEventHandler ActionwebServiceCalled;

    public void CallWebService()
    {
        DownloadStringCompletedEventArgs e = null;

        //Calls web service and does some other operations...

        var handler = ActionwebServiceCalled;
        if (handler != null)
            handler(this, e);
    }
}
话虽如此,您可能希望在web服务调用中引入异步,在这种情况下,只要您有.NET4(),就可以这样做。对于.NET3.5及更低版本,您需要遵循以下步骤