I wrote a code that looks somewhat like this:
Thread t = new Thread(() => createSomething(dt, start, finish) );
t.Start();
And it works (sometimes it almost feel like there are multiple threads).
Yet I don't use any delegates.
- What is the meaning of a tread without a delegate?
- If a delegate is necessary — then please tell me what and how the connection is made to the delegate.
You are using a delegate - this is just C# syntactic sugar for:
The compiler is inferring from the lambda expression and the different overloads that the
Thread
constructor has, that your intent is to:ThreadStart
delegate.Thread
that accepts aThreadStart
object.You could also equivalently write this with anonymous-delegate syntax:
If the arguments to
createSomething
are not (captured) locals, you could write this without anonymous methods altogether, which should highlight the creation of the delegate far more clearly:Multi-threading is very complex. You are cutting and pasting code without even learning anything about the most basic aspects of threading - how to start a thread. Pasting something off the web into a UI to fix or tweak a control, is one thing. This is a completely different kind of process. You need to study the subject, write all your own code, and understand exactly how it works, otherwise you are just wasting your time with this.
A delegate is the .NET version of a type safe function pointer. All threads require an entry point to start execution. By definition when a primary thread is created it always runs Main() as it's entry point. Any additional threads you create will need an explicitly defined entry point - a pointer to the function where they should begin execution. So threads always require a delegate.
Delegates are often used in threading for other purposes too, mainly callbacks. If you want a thread to report some information back such as completion status, one possibility is to create a callback function that the thread can use. Again the thread needs a pointer to be able to execute the callback so delegates are used for this as well. Unlike an entry point these are optional, but the concept is the same.
The relationship between threads and delegates is secondary threads cannot just call methods like the primary app thread, so a function pointer is needed instead and delegates act as function pointers.
You do not see the delegate and you did not create one because the framework is doing it for you in the Thread constructor. You can pass in the method you want to use to start the thread, and the framework code creates a delegate that points to this method for you. If you wanted to use a callback you would have to create a delegate yourself.
Here is code without lambda expressions. SomeClass has some processing that takes a long time and is done on background threads. To help with this the SomeThreadTask has been created, and it contains the process code and everything the thread needs to run it. A second delegate is used for a callback when the thread is done.
Real code would be more complicated, and a real class should never have to know how to create threads etc so you would have manager objects.