Call a Method in a Thread

Bernie

Well-known member
Joined
Aug 13, 2007
Messages
98
Programming Experience
3-5
I have a class that runs as a thread. While the thread is running, I want to call a method in that thread. I do this commonly where a thread calls a method in the GUI with a delegate. But I'm confused how to do this when the called thread is not the GUI and there are no parameters passed to the called method.

Can anyone point me to an example of how this is done?

Thanks,
Bernie
 
First up, classes don't run as threads. An instance of a class is an object and objects simply sit in memory. They don't belong to any specific thread. You might have one instance of a class that has multiple methods executing in multiple threads at the same time.

When you start a new thread, you specify a method as the entry point for that thread. That method will then run in that new thread. That doesn't mean that the object that method is a member of runs in that thread though. The object is just sitting there in memory. You might start a second new thread and specify the very same method as the entry point. That object will then have two instances of the same method running in two different threads.

So, you need to provide a much more detailed explanation of what you're doing and what you're trying to achieve, because I think that your own understanding of the situation is not accurate. It is possible to execute a method in a specific existing thread but it's not trivial and it's rarely necessary. If we understand exactly what you're trying to achieve and why, then we can provide the best advice.
 
I appologize, I've been working with Java for the last months and there you can extend the thread class and have a class run as a thread, my mistake.

I have a class that contains all the methods to process a set of data. My code instantiates the class, and hands it a dataset of the raw data. It then starts a new thread processing the "main" method of this object. Somewhere down the way, while the tread is still processing the data, I need to tell the thread that an event has happened, (based on the user's input.) The thread continues to process the data, but with modications based on the new condition.

So the most straight forward path would be to have the GUI thread, call a method in this class/object that modifies a local variable in the class/object. If was doing this the other way, I would use a delegate and check if the GUI object was thread safe, (I think you are the one that taught me how to do this, hahhaha.) But with the GUI contacting the thread, I don't understand how to pass a delegate with no paramaters and how to check if my method in the thread is safe for cross threading.

Does this make more sense?

Bernie
 
What you're suggesting is not going to work but it doesn't sound like it's needed anyway. You say that you start running this "main" method on a new thread. Data then comes in on another thread and you want this "main" method to use that new data. Raising an event or even invoking a method on that thread isn't going to help you because a single thread can only do one thing at a time, so your "main" method would have to finish before this other method was executed anyway.

I don't think that there's any need for you to worry about threading at all though. As I said, objects don't belong to any specific thread. You can update data on any thread you like and your "main" method will simply carry on using that data. You would likely want to synchronise the two threads to make sure they didn't interfere with each other but, apart from that, there's no issue accessing the same data on multiple threads.
 
Hmmm, so there is no way to make a cross thread call to a method of an object in another thread? Isn't this what we do when a thread calls a method in the main form to update a control on that form? I'm suprised that you can't make the call in the other direction with the main form calling a method in an object on the form.
 
Dumb, dumb, dumb, I already had the example, I just had typed it wrong. I needed an empty delegate;

Public Delegate Sub MyInvoker()
 
First up, if your method has no parameters and doesn't return anything then the .NET Framework already provides the MethodInvoker and Action delegates.

As for post #6, I specifically said in post #2 that you CAN invoke a method in any thread you like. The thing is, you need some reference point on that thread by which to marshal a method call. When you marshal a call to the UI thread using the Invoke method, you're able to do that because all controls implement the ISynchronizeInvoke interface and, generally speaking, all your controls will have been created on the UI thread. You can create an ISynchronizeInvoke instance on any thread you like and do the same thing. Usually though, if you want to marshal to a thread other than the UI thread, you would use the SyncronizationContext class.
 
Just a thought

I have a question just for clarification...

Example:

User 1 running input in thread (5)
User 2 running input in thread (6)
Data process listener running in thread (4)

issue: Need to run send process for Data in a single thread.. ie say (3).

If i use a queue of data requests in a separate thread processing the queue when it has data. I CAN?? add to the queue from user 1 thread (5) and user 2 thread (6) while the queue process in thread (3) sends the data and Thread (4) receives the data and places it in a specialized.namedvaluecollection with the request ID as the key for threads (5) and (6) to watch after they have sent the request??

wow.. that was confusing... but did i get it right? can this happen without any delegates?
 
Data is just data. It simply exists and is accessible by any thread. The same goes for controls for that matter. Any thread can access any control at any time. It's just that some of those calls will fail. So, if you have a Queue, any thread can add to or remove from that Queue at any time. Multiple threads can write to and read from the Queue, but you would need to synchronise access so that those operations didn't occur at exactly the same time and possibly interfere with each other.

The thing is, if you are writing on one thread and reading on another, there's no way that your writer can notify your reader that there's new data available and then have your reader read on a different thread without delegation at some point. Either your writer has to delegate before notifying the reader or else the reader will have to delegate after receiving notification. If you don't then the writing, notification and reading must all occur on the same thread. If you want things to occur on different threads then you have to cross the thread boundary at some point, and that requires delegation.
 
Back
Top