Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you put an object in another thread?

is there any way in c# to put objects in another thread? All I found is how to actually execute some methods in another thread. What I actually want to do is to instanciate an object in a new thread for later use of the methods it provides.

Hope you can help me, Russo

like image 636
Russo Avatar asked Jul 06 '10 08:07

Russo


People also ask

How to access object from another thread c#?

Read() method is not asynchronous (meaning that it will wait until it reads bytes from a client in order to proceed executing code similar to the messagebox method), I have to read for bytes in a separate thread so that the user using the program can still interact with the program if the program happens to be waiting ...

How to start a new thread in c#?

The following code snippet shows how you can create a new thread object using this delegate. Thread t = new Thread(new ThreadStart(MyThreadMethod)); To start the newly created thread, you should call the Start method on the thread object you have created.


2 Answers

Objects do not really belong to a thread. If you have a reference to an object, you can access it from many threads.

This can give problems with object that are not designed to be accessed from many threads, like (almost all) System.Windows.Forms classes, and access to COM objects.

If you only want to access an object from the same thread, store a reference to the thread in the object (or a wrapping object), and execute the methods via that thread.

like image 50
GvS Avatar answered Oct 15 '22 09:10

GvS


There seems to be some confusion about how threads work here, so this is a primer (very short too, so you should find more material before venturing further into multi-threaded programming.)

Objects and memory are inherently multi-thread in the sense that all threads in a process can access them as they choose.

So objects do not have anything to do with threads.

However, code executes in a thread, and it is the thread the code executes in that you're probably after.

Unfortunately there is no way to just "put an object into a different thread" as you put it, you need to specifically start a thread and specify what code to execute in that thread. Objects used by that code can thus be "said" to belong to that thread, though that is an artificial limit you impose yourself.

So there is no way to do this:

SomeObject obj = new SomeObject();
obj.PutInThread(thatOtherThread);
obj.Method(); // this now executes in that other thread

In fact, a common trap many new multi-thread programmers fall into is that if they create an object in one thread, and call methods on it from another thread, all those methods execute in the thread that created the object. This is incorrect, methods always executes in the thread that called them.

So the following is also incorrect:

Thread 1:
    SomeObject obj = new SomeObject();

Thread 2:
    obj.Method(); // executes in Thread 1

The method here will execute in Thread 2. The only way to get the method to execute in the original thread is to cooperate with the original thread and "ask it" to execute that method. How you do that depends on the situation and there's many many ways to do this.

So to summarize what you want: You want to create a new thread, and execute code in that thread.

To do that, look at the Thread class of .NET.

But be warned: Multi-threaded applications are exceedingly hard to get correct, I would not add multi-threaded capabilities to a program unless:

  1. That is the only way to get more performance out of it
  2. And, you know what you're doing
like image 25
Lasse V. Karlsen Avatar answered Oct 15 '22 08:10

Lasse V. Karlsen