none
What is delegate ? RRS feed

  • Question

  • Hello,

    I would like to ask what exactly is delegate and why we use it? In order threads run safely?

    And one mpore thing,could somenone explain to me what is the following code?

    if (dataGridView1.InvokeRequired)
                {
                    var d = new SafeCallDelegate(birthdate);
                    Invoke(d, new object[] { });
                }

    Thank you in advance!

    Saturday, September 14, 2019 10:27 AM

Answers

  • What is a delegate? Basically, it is a pointer to a method. You can think about the delegate as the address or location in memory of a method. So, any piece of code that has the delegate is able to call the method through the delegate. Or, in other words, the delegate would be a way to pass a method as an argument from one part of your code to another.

    If you have used function pointers in other languages such as C, you will recall that they are unsafe because a pointer could be made to point to the wrong location or to the wrong type of function. In C#, however, they are more than simple pointers. The compiler enforces that they can only be initialized to point to a method of the correct type.

    The delegates do not have any relationship to making threads run safely. A thread can be equally safe or unsafe regardless of whether it uses any delegates or not.

    The code that you showed and wanted explained needs to be understood in the context in where it is used. You would normally only use such a block in a Windows Forms application. If you wish, you can launch multiple threads in such an application. However, there is one thing that these threads are not allowed to do: They can not access the User Interface, because the User Interface in Windows forms is not safe for multithreading and its internal structures could be corrupted if it were to be accessed by a thread that is not the same on which the UI was initially created.

    So, what do you do if you need to display something from a secondary thread? Well, there is a method named "Invoke" which is provided for the purpose of marshalling execution from a different thread into the UI thread. So your secondary thread would call the Invoke method and tell it what to execute in the main thread. How do you tell it what to execute? You write it in a method and pass the method as an argument to "Invoke". You see what we are getting to, don't you? How do you pass a method as an argument? Right, we just mentioned it before: you use a delegate that points to the method. That's exactly what the code that you showed is doing.

    In that code, "birthdate" is the name of the method that has to be executed in the main tread. new SafeCallDelegate(birthdate) initializes a delegate of type SafeCallDelegate and makes it point to birthdate. So now variable "d" is a delegate pointing to the function. You then call Invoke and pass d as an argument. Invoke can then use the value that it receives to execute birthdate.

    The call to "InvokeRequired" in the initial "if" basically means "is this code executing from a secondary thread?" You use it because if you were in the main thread then you wouldn't need the call to Invoke. This "if" could be omitted f you are certain (given the structure of your code) that it is always going to be executed from a secondary thread.


    Sunday, September 15, 2019 12:33 PM
    Moderator

All replies

  • Saturday, September 14, 2019 10:34 AM
  • What is a delegate? Basically, it is a pointer to a method. You can think about the delegate as the address or location in memory of a method. So, any piece of code that has the delegate is able to call the method through the delegate. Or, in other words, the delegate would be a way to pass a method as an argument from one part of your code to another.

    If you have used function pointers in other languages such as C, you will recall that they are unsafe because a pointer could be made to point to the wrong location or to the wrong type of function. In C#, however, they are more than simple pointers. The compiler enforces that they can only be initialized to point to a method of the correct type.

    The delegates do not have any relationship to making threads run safely. A thread can be equally safe or unsafe regardless of whether it uses any delegates or not.

    The code that you showed and wanted explained needs to be understood in the context in where it is used. You would normally only use such a block in a Windows Forms application. If you wish, you can launch multiple threads in such an application. However, there is one thing that these threads are not allowed to do: They can not access the User Interface, because the User Interface in Windows forms is not safe for multithreading and its internal structures could be corrupted if it were to be accessed by a thread that is not the same on which the UI was initially created.

    So, what do you do if you need to display something from a secondary thread? Well, there is a method named "Invoke" which is provided for the purpose of marshalling execution from a different thread into the UI thread. So your secondary thread would call the Invoke method and tell it what to execute in the main thread. How do you tell it what to execute? You write it in a method and pass the method as an argument to "Invoke". You see what we are getting to, don't you? How do you pass a method as an argument? Right, we just mentioned it before: you use a delegate that points to the method. That's exactly what the code that you showed is doing.

    In that code, "birthdate" is the name of the method that has to be executed in the main tread. new SafeCallDelegate(birthdate) initializes a delegate of type SafeCallDelegate and makes it point to birthdate. So now variable "d" is a delegate pointing to the function. You then call Invoke and pass d as an argument. Invoke can then use the value that it receives to execute birthdate.

    The call to "InvokeRequired" in the initial "if" basically means "is this code executing from a secondary thread?" You use it because if you were in the main thread then you wouldn't need the call to Invoke. This "if" could be omitted f you are certain (given the structure of your code) that it is always going to be executed from a secondary thread.


    Sunday, September 15, 2019 12:33 PM
    Moderator