質問

Class Client{
  main(){
    MyRequest m = new MyRequest();
    m.function();
  }
  onSucess(string s){
    Debug.log("i get data from network:"+s);
  }
}
Class Network{
   sendMyrequest(MyRequest r){
     Thread thread = new Thread(() => sendMyrequestTask(r));
     thread.start(); 
   }
   private void sendMyrequestTask(MyRequest r){
     if(...){
        //call delegate function onSucess(string s)
     }
   }
}
Class MyRequest{
private Network network;
  function(){
    //do something
    network.sendMyrequest(MyRequest r);

  }

}

in this case, callback function onSucess(string s) should be a delegate, or a interface, how and where should I implement it? Any suggestion would be appreciate. Thanks in advance!!

Edit: this problem is like: A call B,B call C, when C's job is done, C should call A. How to implement this?

Thanks all guys. I implement in this way.

public interface CallbackFunction{
   public onSucess(string s);
}
Class Client:CallbackFunction{
  main(){
    MyRequest m = new MyRequest();
    m.function(this);
  }
  onSucess(string s){
    Debug.log("i get data from network:"+s);
  }
}
Class Network{
   sendMyrequest(MyRequest r,CallbackFunction c){
     Thread thread = new Thread(() => sendMyrequestTask(r,c));
     thread.start(); 
   }
   private void sendMyrequestTask(MyRequest r,CallbackFunction c){
     if(...){
        //call delegate function onSucess(string s)
        c.onSucess("bla bla bla");
     }
   }
}
Class MyRequest{
private Network network;
  function(CallbackFunction c){
    //do something
    network.sendMyrequest(this,c);

  }

}
役に立ちましたか?

解決 2

By using the Action<> delegate as a constructor argument in MyRequest we can achieve something like this.

You can replace the Action<> type with any other delegate you might want to use. Action<> or Func<> should cover just about anything.

By the way, your code, and hence my code below is not the observer pattern.

public class Client
{
    static void Main(string[] args)
    {
        var client = new MyRequest(OnSuccess);
        client.Function();
        //Output:
        //I'm in the callback
        //Foo.Bar()
        Console.ReadKey();
    }

    static void OnSuccess(string result)
    {
        Console.WriteLine("I'm in the callback");
        Console.WriteLine(result);
    }

}

public class Network
{
    public void SendMyRequest(MyRequest request)
    {
        var result = "Foo.Bar()";

        if (!String.IsNullOrEmpty(result))
        {
            request.SuccessCallback(result);
        }
    }
}

public class MyRequest
{
    public Action<string> SuccessCallback { get; private set; }
    private Network _network;

    public MyRequest(Action<string> successCallback)
    {
        _network = new Network();
        SuccessCallback = successCallback;
    }
    public void Function()
    {
        _network.SendMyRequest(this);
    }
}

他のヒント

You can use async and `await.

For more info :

Many methods do not immediately return. A method may need to query an external source. This takes time. With async and await, we formalize and clarify how asynchronous, non-blocking methods begin and end.

http://msdn.microsoft.com/en-us/library/hh191443.aspx

http://msdn.microsoft.com/en-us/library/hh156513.aspx

It looks like you want to implement something similar to WebClient.DownloadStringCompleted event. Which uses following delegate type:

public delegate void DownloadStringCompletedEventHandler(
    Object sender
    DownloadStringCompletedEventArgs e)`

with following usage pattern:

WebClient client = new WebClient ();
client.DownloadStringCompleted += DownloadStringCallback2;
client.DownloadStringAsync (new Uri(address));

Note: if you don't expect multiple listeners using async/await will lead to much easier to understand code.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top