如何从线程中运行的类中获取公共变量 [英] How to obtain public variables from a class being run in a thread

查看:86
本文介绍了如何从线程中运行的类中获取公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有运行具有公共变量的类的线程。我希望能够从主线程的线程中获取类中的公共变量。以下是我所拥有的摘要:





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)

推荐答案

基于我在过去的答案中提出的 ThreadWrapper 有一个优雅的解决方案:

如何将ref参数传递给线程 [< a href =http://www.codeproject.com/Answers/155852/How-to-pass-ref-parameter-to-the-thread#answer2target =_ blanktitle =New Window> ^ ],

更改线程(生产者)启动后的参数 [ ^ ],

C#中的MultiThreading [ ^ ]。



这个想法是基于线程启动方法可以是静态方法还是实例方法(非静态)的事实。在实例方法的情况下,它可以是包装器实例方法。这样,您可以将所有参数作为包装器属性传递,甚至可以方便地封装thred synchronizatoin,这样调用者就不需要使用synchronizatoin进行控制。



CodeProject成员很高兴提供VB.NET版本:将参数传递给线程LongRunningProcess [ ^ ]。



但是,我在答案中介绍了更重要的技巧。如果需要,您可以轻松地将我的C#示例自动转换为VB.NET。我在过去的答案中解释了如何做到这一点。请参阅:

FixedPage to ContentPage将c#代码转换为vb.net [ ^ ],

需要将vb代码转换为c# [ ^ ]。







在OP澄清问题后。



为了更好地理解,请看我对 Thread.Abort 的评论以及我过去的答案,我试图解释其非平凡的机制:



正确关闭dll中的线程 [ ^ ],

从专用线程创建流程的问题 [ ^ ]。



祝你好运,

-SA
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#[^].



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


这篇关于如何从线程中运行的类中获取公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆