2

If I have a created a object like this

memberdetail md = new memberdetail(arg0, arg1, arg3)

how can I create a thread for the md object?

Thread t = new Thread((md));
t.Start();

is not working. Thx

3
  • 1
    In this scenario, you need to point your thread to method in your md instance. The ThreadStart needs to point to a method, not an "object". Commented Aug 5, 2011 at 14:01
  • 1
    Are you trying to pass md as a parameter to a Thread? If so you would need to use a Parameterized ThreadStart msdn.microsoft.com/en-us/magazine/cc163600.aspx. Commented Aug 5, 2011 at 14:03
  • You can't create a thread for an object; you’d rather define a task (method) which you want to run in separate thread. Commented Aug 5, 2011 at 14:03

7 Answers 7

3

You don't start a thread on an object, but rather a method:

memberdetail md = new memberdetail(arg0, arg1, arg3);
Thread t = new Thread(md.DoSomething);   
t.Start();
Sign up to request clarification or add additional context in comments.

2 Comments

You can start a Thread with a specific object-method but there is no link between thread and object whatsoever.
Yup! Rightly said. Updated my answer to remove this implying statement.
3

You have to pass the object's method to the Thread's constructor as below,

Thread t = new Thread(md.SomeFn);
t.Start();

http://msdn.microsoft.com/en-us/library/ms173178(v=vs.80).aspx

Comments

2

If you want to pass the object into the thread on start, do this:

public class Work
{
    public static void Main()
    {
        memberdetail md = new memberdetail(arg0, arg1, arg3)
        Thread newThread = new Thread(Work.DoWork);

        // Use the overload of the Start method that has a
        // parameter of type Object.
        newThread.Start(md);
    }

    public static void DoWork(object data)
    {
        Console.WriteLine("Static thread procedure. Data='{0}'", data);
        // You can convert it here
        memberdetail md = data as memberdetail;
        if(md != null)
        {
           // Use md
        }
    }
}

See Thread.Start Method (Object) for more details.

Comments

1
  1. You do not create a Thread for an object. Threads are separate.

  2. In most cases you should not be using Threads (anymore). Look at ThreadPool and Tasks (TPL library).

2 Comments

+1 for point 3, The thread pool usually has significant advantages to hand rolling.
Would any one pls take a look at this question if you know any thing pls give me any suggestions stackoverflow.com/questions/6953471/…
1

Try this if all you want to do is create multiple objects in different threads.

for(int i = 0, i < numThreads; i++)
    (new Thread( () => 
        { memberdetail md = new memberdetail(arg0, arg1, arg3) }).start()

Any other actions you want to perform you can do inside the body of the lambda e.g.

for(int i = 0, i < numThreads; i++)
    (new Thread( () => 
        {
            memberdetail md = new memberdetail(arg0, arg1, arg3);
            md.ActionOne();
            md.ActionTwo();
            //Some other actions...
        }).start()

Comments

0

You can't create a thread for objects per se. You can pass an instance of a delegate to be invoked on the thread.

like on MSDN.

Threads do functions not, contain objects.

Comments

0

I would recommend using ThreadPool.QueueUserWorkItem so that the application can manage the thread, and make sure memberdetail inherits from object.

http://msdn.microsoft.com/en-us/library/4yd16hza.aspx

   memberdetail md = new memberdetail(arg0, arg1, arg3)
   ThreadPool.QueueUserWorkItem(DoWork, md);

    private void DoWork(object state)
    {
       if (state is memberdetail)
       {
         var md= state as memberdetail;    
         //DO something with md
       }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.