开发者

Help with understanding C# syntax while Invoking a new Action

开发者 https://www.devze.com 2023-03-18 05:56 出处:网络
I am new to c# and do not understand the syntax of invoking a new action or even what an action is. From my understanding in Port1_DataReceived, I have to create an action because I am in a new tread.

I am new to c# and do not understand the syntax of invoking a new action or even what an action is. From my understanding in Port1_DataReceived, I have to create an action because I am in a new tread... Can anyone elaborate on why I need to do this?

public Form1()
{
    InitializeComponent();
    S开发者_如何学运维erialPort Port1 = new SerialPort("COM11", 57600, Parity.None, 8, StopBits.One);
    Port1.DataReceived += new SerialDataReceivedEventHandler(Port1_DataReceived);
    Port1.Open();
}


private void Port1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
     SerialPort Port = (SerialPort)sender;
     string Line = "";
     int BytestoRead = Port.BytesToRead;
     Line = Port.ReadLine();
     label1.Invoke(new Action(() =>
     {
          label1.Text = Line;
      }));
}

The code snip that I am really having trouble understanding is:

label1.Invoke(new Action(() =>
         {
              label1.Text = Line;
          }));

Can someone break down what this is doing.. I am sure it is nothing to complicated, just that I have never seen anything like it before. The syntax that is really holding me up is ()=> the new action is pointing to the code below or something??


This uses something known as a "lambda expression" to create an anonymous delegate that matches the signature expected by the Action constructor.

You could achieve the same effect like this:

label1.Invoke(SetText);
...
public void SetText() { label1.Text = Line; }

or like this:

label1.Invoke(new Action(SetText));
...
public void SetText() { label1.Text = Line; }

or like this:

label1.Invoke(new Action(delegate() { label1.Text = Line; }));

or like this:

label1.Invoke(delegate() { label1.Text = Line; });

or like this:

label1.Invoke(() => label1.Text = Line);

These are mostly just syntactic shortcuts to make it easier to represent an action.

Note that lambda expressions often have parameters. When there is only one parameter, the parentheses are optional:

list.ToDictionary(i => i.Key);

When there are no parameters or multiple parameters, the parentheses are necessary to make it obvious what you're doing. Hence, the () =>.


Let's break it down piece by piece.

label1.Invoke(

This is the Control.Invoke method. Here's how it's defined:

public Object Invoke(Delegate method);

Executes the specified delegate on the thread that owns the control's underlying window handle.

What that means is that you give it a reference to a method to call, and Control.Invoke will make sure it gets called on the UI thread (which will prevent cross-threading exceptions while updating the UI.) It takes a default Delegate as a parameter, which means you need to pass it a method that takes no parameters and has no return value. That's where the System.Action delegate type comes in:

public delegate void Action();

Using lambda expressions, we can create an Action delegate inline. First, we specify the delegate type:

label1.Invoke(new Action(

Then, we will begin the lambda syntax. An empty set of parenthesis will denote that the lambda function takes no parameters, and an "arrow" afterwards shows that we want to start the method:

label1.Invoke(new Action(() =>

Now, because the lambda method has no return value (but must execute a statement) we need to surround the code we want to execute on the UI thread in curly braces:

label1.Invoke(new Action(() =>
{
    label1.Text = Line;
}

Close up the remaining parenthesis, and you have the full, finished statement.

label1.Invoke(new Action(() =>
{
    label1.Text = Line;
}));


Generally when you want to add something to you GUI and you are working from another thread you need to do something called Invocation.

To make an invocation you use either a Controls Invoke method or the something like an Application Dispatcher, these methods generally take an Action. An Action is just what it sounds like, something that is to be performed.

In your case what you are doing is that you want to add a line of text to an element that lives on your GUI, so what you need to do is to create an Action ( anonymouse method ) and in this action you just say "Add this to my Control". And then you Invoke this to avoid cross-threading problems.

()=> is just a "shortcut"(lambda way) to create a method, that is anonymous. This means that you can't call this from anywhere but the context of where you created the anonymous method.

You can also Invoke a "global" method, it doesn't have to be an anonymous method.


An Action is a delegate type, in other words it encapsulates a function. Specifically an Action encapsulates a function that returns void, whereas for instance a Func would encapsulate a function with a return value. These are alot like a function pointers in C++ -- essentially a reference to a function ie a way to encapsulate behavior.

The .Invoke() method takes the Action delegate and runs the function it points to. In this case the function it points to is the lambda expression:

() => { label1.Text = Line }

The initial parentheses denote any parameters being passed into the function. In this case there are no parameters so the parentheses are empty. For example if you wanted to pass in two strings, you would do:

var action = new Action<string, string>( (x, y) => { // use x and y }

Whatever follows the '=>' expression is essentially the body of the function. You have access to the variables specified in the parentheses inside the scope of this body.

Altogether this is a quick way to create an anonymous function on the fly that essentially is equivalent to the following:

public void SetLine()
{
   label1.Text = Line; 
}

As such you could also create that Action object by doing:

var action = new Action(SetLine) 

where you are passing in the name of the method to encapsulate instead of passing in a lambda. Whats passed in is known as a 'Method Group'.


Action is a delegate. Label1.Invoke() is being used to execute the code label1.Text = line to avoid Cross Threading Operation. the event handler for DataReceived event is executing on a different thread other than UI thread. label1.Invoke() will execute the code in UI thread.


This is generating an anonymous method (a lambda, precisely) and passing that to the invoke method. Lambdas are a great way to have code you only need once so you don't need a lot of helper methods doing one thing only.


This is ensuring that the label's text is running in the UI thread. The Port1_DataReceived event will likely run in a background thread, and the Label's text value should not be set from background threads. This prevents that from happening.


I don't know what the label1 is, but the could could be read as:

label1 is an Action, that recieves another action as parameter. It does something and when it calls action recieved in argument.

Now, I've read that and I could be a problem - label1 could not be an Action. As it is just a control which set here: label1.Text = Line;

You have an error in your app;

EDIT

Sorry, just read that:

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

Executes the specified delegate on the thread that owns the control's underlying window handle.

Code is correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消