代码之家  ›  专栏  ›  技术社区  ›  Malfist

C中的回调#

  •  20
  • Malfist  · 技术社区  · 16 年前

    我该怎么做?

    5 回复  |  直到 16 年前
        1
  •  43
  •   T.J. Crowder    6 年前

    您有两个选择:

    1. 让函数接受 delegate ( Action Func

    2. 使用界面

    使用委托/lambda

    public static void DoWork(Action processAction)
    {
      // do work
      if (processAction != null)
        processAction();
    }
    
    public static void Main()
    {
      // using anonymous delegate
      DoWork(delegate() { Console.WriteLine("Completed"); });
    
      // using Lambda
      DoWork(() => Console.WriteLine("Completed"));
    }
    

    如果你的回调需要传递一些东西,你可以在 行动 :

    public static void DoWork(Action<string> processAction)
    {
      // do work
      if (processAction != null)
        processAction("this is the string");
    }
    
    public static void Main()
    {
      // using anonymous delegate
      DoWork(delegate(string str) { Console.WriteLine(str); });
    
      // using Lambda
      DoWork((str) => Console.WriteLine(str));
    }
    

    如果它需要多个参数,您可以向添加更多类型参数 行动 。如果你需要返回类型,如前所述,请使用 函数 并将return类型设置为 最后的 类型参数( Func<string, int> 是一个接受字符串并返回整数的函数。)

    更多关于代表的信息 here .

    public interface IObjectWithX
    {
      void X();
    }
    
    public class MyObjectWithX : IObjectWithX
    {
      public void X()
      {
        // do something
      }
    }
    
    public class ActionClass
    {
      public static void DoWork(IObjectWithX handlerObject)
      {
        // do work
        handlerObject.X();
      }
    }
    
    public static void Main()
    {
      var obj = new MyObjectWithX()
      ActionClass.DoWork(obj);
    }
    
        2
  •  6
  •   Jon Skeet    16 年前

    object 或者可能使用通用委托并获取适当类型的状态,例如。

    public delegate void Callback<T>(T state, OperationResult result)
    

    然后:

    public void DoSomeOperation(int otherParameterForWhateverReason,
                                Callback<T> callback, T state)
    

    正如你正在使用的那样。NET 3.5,您可能希望使用现有的 Func<...> Action<...> 可能

        3
  •  1
  •   Ed Swangren    16 年前

        4
  •  1
  •   Cory McCarty    16 年前

    有没有理由不让你的图书馆在操作完成时提供公开活动?然后,调用者可以注册来处理事件,而不必担心传递对象或委托。

        5
  •  -1
  •   Vijay Bansal    9 年前

    你可以使用系统。C#中提供的操作。NET用于回调函数。请查看此示例:

        //Say you are calling some FUNC1 that has the tight while loop and you need to 
        //get updates on what percentage the updates have been done.
        private void ExecuteUpdates()
        {
            Func1(Info => { lblUpdInfo.Text = Info; });
        }
    
        //Now Func1 would keep calling back the Action specified in the argument
        //This System.Action can be returned for any type by passing the Type as the template.
        //This example is returning string.
        private void Func1(System.Action<string> UpdateInfo)
        {
            int nCount = 0;
            while (nCount < 100)
            {
                nCount++;
                if (UpdateInfo != null) UpdateInfo("Counter: " + nCount.ToString());
                //System.Threading.Thread.Sleep(1000);
            }
        }