views:

158

answers:

2

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.

  1. What is the meaning of a tread without a delegate
  2. If a delegate is necessary - then please tell me what and how the connection is made to the delegate.
+9  A: 

You are using a delegate - this is just C# syntactic sugar for:

Thread t = new Thread(new ThreadStart( () => createSomething(dt, start, finish))); 
t.Start();

The compiler is inferring from the lambda expression and the different overloads that the Thread constructor has, that your intent is to:

  • Create an instance of the ThreadStart delegate.
  • Pass it as an argument to the constructor overload of Thread that accepts a ThreadStart object.

You could also equivalently write this with anonymous-delegate syntax:

 Thread t = new Thread(delegate() { createSomething(dt, start, finish); } ); 
 t.Start();

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:

private void Create()
{
   createSomething(dt, start, finish))); 
}

...

Thread t = new Thread(new ThreadStart(Create)); //new ThreadStart is optional for the same reason 
t.Start();
Ani
Thanks, beautiful things happen when you copy code from smart people, yet this "sugar" hides the "normal" way from me... I don't declare a delegate and surely don't understand how it works...So you helped me to understand that my program works - because it is written correctly, yet I don't understand what happens under the hood ... what I really need is a toturial\example for not "sugar" for 'calling a function with no param' and 'calling a function with params'
Asaf
The lambda `() => createSomething(dt, start, finish)` is equivalent to `new delegate() { return createSomething(dt, start, finish); }`. If the delegate returns `void` shouldn't the lambda look like: `() => {createSomething(dt, start, finish);}`? Other thing is if the use of such lambda is legal here.
Maciej Hehl
@Maciej Hehl: 1. The braces and semi-colon are optional. 2. Lamba expressions can always substitute for anonymous delegates.
Ani
It's not just optional. The first one is expression lambda and the second one is a statement lambda. Those are two types of lambdas and there are some restrictions on using some types in some circumstances. One is that statement lambdas can't be used to create expression trees, but I don't remember if there are others. Maybe statement lambda is legal here. If `createSomething` returns void, they might be equivalent, but is it?
Maciej Hehl
Yes it is, `ThreadStart` is declared as: `public delegate void ThreadStart();` That's what makes it optional in this case.
Ani
+2  A: 

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.

// Create a delegate for our callback function.
public delegate void SomeThreadTaskCompleted(string taskId, bool isError);


public class SomeClass
{

    private void DoBackgroundWork()
    {
        // Create a ThreadTask object.

        SomeThreadTask threadTask = new SomeThreadTask();

        // Create a task id.  Quick and dirty here to keep it simple.  
        // Read about threading and task identifiers to learn 
        // various ways people commonly do this for production code.

        threadTask.TaskId = "MyTask" + DateTime.Now.Ticks.ToString();

        // Set the thread up with a callback function pointer.

        threadTask.CompletedCallback = 
            new SomeThreadTaskCompleted(SomeThreadTaskCompletedCallback);


        // Create a thread.  We only need to specify the entry point function.
        // Framework creates the actual delegate for thread with this entry point.

        Thread thread = new Thread(threadTask.ExecuteThreadTask);

        // Do something with our thread and threadTask object instances just created
        // so we could cancel the thread etc.  Can be as simple as stick 'em in a bag
        // or may need a complex manager, just depends.

        // GO!
        thread.Start();

        // Go do something else.  When task finishes we will get a callback.

    }

    /// <summary>
    /// Method that receives callbacks from threads upon completion.
    /// </summary>
    /// <param name="taskId"></param>
    /// <param name="isError"></param>
    public void SomeThreadTaskCompletedCallback(string taskId, bool isError)
    {
        // Do post background work here.
        // Cleanup the thread and task object references, etc.
    }
}


/// <summary>
/// ThreadTask defines the work a thread needs to do and also provides any data 
/// required along with callback pointers etc.
/// Populate a new ThreadTask instance with any data the thread needs 
/// then start the thread to execute the task.
/// </summary>
internal class SomeThreadTask
{

    private string _taskId;
    private SomeThreadTaskCompleted _completedCallback;

    /// <summary>
    /// Get. Set simple identifier that allows main thread to identify this task.
    /// </summary>
    internal string TaskId
    {
        get { return _taskId; }
        set { _taskId = value; }
    }

    /// <summary>
    /// Get, Set instance of a delegate used to notify the main thread when done.
    /// </summary>
    internal SomeThreadTaskCompleted CompletedCallback
    {
        get { return _completedCallback; }
        set { _completedCallback = value; }
    }

    /// <summary>
    /// Thread entry point function.
    /// </summary>
    internal void ExecuteThreadTask()
    {
        // Often a good idea to tell the main thread if there was an error
        bool isError = false;

        // Thread begins execution here.

        // You would start some kind of long task here 
        // such as image processing, file parsing, complex query, etc.

        // Thread execution eventually returns to this function when complete.

        // Execute callback to tell main thread this task is done.
        _completedCallback.Invoke(_taskId, isError);


    }

}

}

Sisyphus
First I appreciate your detailed answer. I will take my time to learn it carefully. About the copy and paste... Sometimes a football player that learns his football in the neibourhood and then perfected by a good club is better then one that grew up in a club. In other words sometimes one needs to feel something before understanding it. Anyway... You helped me and I thank you for that
Asaf