怎么看 看不懂啊 RRS feed

  • 问题

  •   Dispatcher.Invoke(new Action(() =>   //子线程中实现控制UI 

                            for (int i = 0; i < 60; i++)  //重新生成数据
                                TempData.Add(new DataPoint(i, tempValue[i]));
                            myPlot.InvalidatePlot(true);  //曲线刷新

    Action  怎么用  为什么会有for循环

    2018年7月11日 6:33


  • Action是一个委托,不需要参数,也没有返回值,参考下面的.NET 委托进化史


    I think you're overthinking things a little bit. So let's start from the "margin:0px 0px 1em 30px;padding:0px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;line-height:inherit;font-family:Arial, 'Helvetica Neue', Helvetica, sans-serif;font-size:15px;vertical-align:baseline;list-style-list-style-image:initial;box-sizing:inherit;color:#242729;">

  • A lambda expression is a notation to reference a method execution. Example:

     x => x + 3

    At the most basic level, this is representing a function that takes 1 input, x, and then returns a value equal to x + 3. So in your situation, your expression:

    () => DoSomething(15, "Something")

    Represents a method taking 0 parameters, and then invoking the method DoSomething(15, "Something"). The compiler is behind the scenes translating that into a Func or Actiondelegate for you. So it is in effect:

    new Action(delegate()
        DoSomething(15, "Something")

    The compiler rewrite of my simple expression above would be:

    new Func<int, int>(delegate(int x)
        return x + 3;
  • Next up, if you want to invoke an action later, the syntax for doing so is fairly straightforward:

    Action someMethod = new Action(() => { Console.WriteLine("hello world"); }));
    someMethod(); // Invokes the delegate

    So if you have a given Action instance, simply invoking it with the () syntax is all you need, since Action is a delegate that takes 0 parameters and returns nothing.

    A function is similarly easy:

    Func<int, int> previousGuy = x => x + 3;
    var result = previousGuy(3); // result is 6
  • Lastly, if you want to pass along a method to invoke, and you don't have context for the parameters at that point, you can simply wrap your call in an action and invoke that later. For example:

    var myAction = new Action(() =>
              // Some Complex Logic
              DoSomething(15, "Something");
              // More Complex Logic, etc
    public void InvokeLater(Action action)

    All of the data is captured in a closure of your method, and thus is saved. So if you can manage to pass along an Action to your event with the e.Argument property, all you would need to do would be to call (e.Argument as Action)().

  • 专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

2018年7月12日 0:19
  • 既然是委托。委托的内容就完全是一个匿名的方法,可以写符合语言要求的C#语句。



    static void Main(string[] args)
        Action doWorkAction = new Action(DoWork);
        doWorkAction(); //Print "Hi, I am doing work."
    public static void DoWork()
        Console.WriteLine("Hi, I am doing work.");

    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年7月12日 0:21
  • 你好,

    Action 是一个无返回值得泛型委托,这是一种简写模式,你也可以这样写,好理解一点:

    Dispatcher.Invoke(new Action(Test));
    public void Test()
            for (int i = 0; i < 60; i++)  //重新生成数据
                     TempData.Add(new DataPoint(i, tempValue[i]));
          myPlot.InvalidatePlot(true);  //曲线刷新

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年7月12日 6:47