ThreadStart Delegate in C sharp Arabic #86
Suggested Videos
❑ The purpose of ThreadStart delegate.
ThreadStart Delegate
To create a THREAD, Create an instance of Thread class and to it’s constructor pass the name of the function that we want the thread to execute.
We can rewrite the above line using ThreadStart delegate as shown below.
Why a delegate need to be passed as a parameter to the thread class constructor ? The purpose of creating a Thread is to execute a function. A delegate is type safe function pointer ,meaning it points to function that the thread has to execute . In short , all threads require an entry point to start execution .Any thread you create will need an explicitly defined entry point i.e a pointer to the function where they should begin execution .So threads always require a delegate .
In the code below , we are not explicitly creating the ThreadStart delegate, then how is it working here?
It ‘s working in spite of not creating the ThreadStart delegate explicity because the framework is doing .
it automatically for us.
We can also rewrite the same line using delegate () keyword.
same line rewritten using lambda expression.
Thread function need not be a static function always . It can also be a nonstatic function , In case of non-static function we have to create an instance of the class.
Tags:
C sharp