Click here to Skip to main content
16,017,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have threads that are running a class that has public variables. I want to be able to get at the public variables from the class in the thread from the main thread. Here is a summary of what I have:


(Main Class Header Area)
---------------------------------
Public ThreadSender(10) As Thread


(Thread Creation Area)
---------------------------------
Dim t As New clsThreadSender
lUnusedindex=GetFreeIndex()
ThreadSender(lUnusedindex) = New Thread(AddressOf t.SendCampaign)
t.StopFlag = False
ThreadSender(lFoundIndex).Priority = System.Threading.ThreadPriority.Normal
ThreadSender(lUnusedindex).Start()


From the main program, how can I get at the public variables in a thread? (I know this wont work but something like this):

msgbox(ThreadSender(3).TheClassPublicVariable)
Posted
Updated 31-May-13 13:13pm
v2

1 solution

There is an elegant solution based on the ThreadWrapper I presented in my past answers:
How to pass ref parameter to the thread[^],
Change parameters of thread (producer) after it is started[^],
MultiThreading in C#[^].

The idea is based on the fact that thread start method could be either static or instance method (not-static). In case of instance method, it could be the wrapper instance method. This way, you can pass all parameters as wrapper properties and even conveniently encapsulate thred synchronizatoin, so the caller won't need to be conserned with synchronizatoin.

CodeProject member was so nice to provide a VB.NET version: Passing arguments to a threaded LongRunningProcess[^].

However, I covered more important techniques in my answers. If you want, you can easily translate my C# samples into VB.NET automatically. I explained how to do it in my past answers. Please see:
FixedPage to ContentPage convert c# code into vb.net[^],
Need to convert vb code to c#[^].

[EDIT]

After OP's clarification of the problem.

For better understanding, please see my comments on Thread.Abort and my past answers where I tried to explain its non-trivial mechanism:

Close correcly the thread inside a dll[^],
Problem with creating Process from dedicated thread[^].

Good luck,
—SA
 
Share this answer
 
v2
Comments
onemorecoke 1-Jun-13 11:33am    
SA, thank you for your answer, but I must admit I am not advanced enough in my knowledge of threading to understand most of the concepts. My main goal was to cause a thread to shut down normally without using .abort, but rather by setting a flag that would cause the thread loop to exit. I had seen in some examples setting up an overall global variable that that the main program would set when it wanted the threads to shut down (all of them), but in my situation I wanted to just shut down one thread of the 10 that were running. I thought about it more and maybe a more simplier solution for me would be to have a global boolean array with the the index being the thread id I assigned when the thread started. I could have each thread just monitor it's own "stop" flag by looking at the array element that pertains to it. The main program could set the array element of the thread it wants to shut down and then on the next loop the thread would see this bit set and then shut down. What do you think? I know there are probably more eligant solutions, as you have shown, but for someone like me this seems to be something I could understand right now and would seem to be a solution for this specific issue. What do you think? I am going to accept your answer because it may help others in the future.
Sergey Alexandrovich Kryukov 1-Jun-13 21:50pm    
If you are not advanced enough, first make yourself advanced enough, or, in the meanwhile, do something a bit simpler, to collect some knowledge, experience and get confidence. Thank you for accepting my answer. This technique is really simpler than others. If you try the sample code I demonstrated, you can see that other methods involves more trouble. And yes, it's good to pass exit condition.

What you are trying to do (stop flag) is the preferring safe method to make a thread stop, but it only works in a number of simple scenarios, when testing such flag on regular basis is feasible. There are cases when it is not. If your thread algorithm allows that — good for you.

Using Abort is highly discussable matter, but I think many opponents of the Abort does not really understand how it works. This mechanism is not trivial at all, but it can be used in a very reliable way; I must admit that you should understand really well what you are doing. I explain the mechanism in my past answers. Please see the update, above, after [EDIT].

—SA
Sergey Alexandrovich Kryukov 1-Jun-13 21:53pm    
So, if you still need some help on passing the "stop flag" (which does make sense, as I admitted above), your follow-up questions are welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900