Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with understanding C# syntax while Invoking a new Action

Tags:

c#

action

invoke

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();
    SerialPort 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??

like image 806
Richard Avatar asked Jul 07 '11 16:07

Richard


People also ask

Is C easy to understand?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C tough to learn?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

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 () =>.

like image 69
StriplingWarrior Avatar answered Sep 29 '22 23:09

StriplingWarrior