Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : this.Invoke((MethodInvoker)delegate

can somebody explain me the following code please :

                this.Invoke((MethodInvoker)delegate
                {
                    lblNCK.Text = cncType;
                });

Here is where it comes from :

        string cncType;

        if (objDMainCncData != null)
        {
            int rc = objDMainCncData.Init(objDGroupManager.Handle);

            if (rc == 0)
            {
                cncType = objDMainCncData.GetCncIdentifier();

                if (cncType != string.Empty)
                {
                    if (cncType.ToUpper().IndexOf("+") != -1)
                        _bFXplus = true;

                    this.Invoke((MethodInvoker)delegate
                    {
                        lblNCK.Text = cncType;
                    });
                }
            }
            else
            {
                DisplayMessage("objDMainCncData.Init() failed ! error : " + rc.ToString());
            }
        }
    }

I don't get the use of "this.Invoke((MethodInvoker)delegate".

Thank you by advance.

Peter.

like image 693
Peter Avatar asked Apr 07 '16 09:04

Peter


People also ask

Is C language easy?

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.

What is C in simple words?

C Introduction C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


1 Answers

Strange that no one has answered this.

Lets take it in pieces:

this.Invoke: This is a synchronization mechanism, contained in all controls. All graphic/GUI updates, must only be executed from the GUI thread. (This is most likely the main thread.) So if you have other threads (eg. worker threads, async functions etc.) that will result in GUI updates, you need to use the Invoke. Otherwise the program will blow up.

delegate{ ... }: This is a anonymous function. You can think of it as "creating a function on the fly". (Instead of finding a space in the code, create function name, arguments etc.)

(MethodInvoker): The MethodInvoker is just the name of the delegate, that Invoke is expecting. Eg. Invoke expects to be given a function, with the same signature as the "MethodInvoker" function.

What happens, is that Invoke is given a function pointer. It wakes up the GUI thread through a mutex and tells it to executes the function (through the function pointer). The parent thread then waits for the GUI thread to finish the execution. And it's done.

like image 123
Illishar Avatar answered Oct 20 '22 03:10

Illishar