Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1097655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:27:54+00:00 2026-05-17T00:27:54+00:00

I wrote a code that looks somewhat like this: Thread t = new Thread(()

  • 0

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.
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:27:55+00:00Added an answer on May 17, 2026 at 12:27 am

    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);
    
    
        }
    
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote some code that looks like this: def get(x, y) @cachedResults.set(x,y, Math.hypot(x, y))
I'm trying to write a regular expression for html code that looks like this:
I wrote this code that compiles on Solaris gcc it works fine too for
I wrote a function that looks like this: - (void)changeText:(NSUInteger)arrayIndex; Let's just say that's
As an experiment, I wrote some code that looks like class MyClass @var =
I wrote this piece of code that is supposed to redirect something written on
I was given a block of c++ code that looks like it was from
I have a class that looks like this: public class UploadBean { protected UploadBean(Map<String,?>
I have code that looks like the following in a class that extends MembershipProvider
I have a copy constructor that looks like this: Qtreenode(const QtreeNode * & n)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.