帮助使用vb.net声明全局变量 [英] help declaring a global variable with vb.net

查看:114
本文介绍了帮助使用vb.net声明全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主窗体上有一个文本框,我放置了一个远程计算机名称.我有一个恶意的声明,我将public strcomputer声明为string = machinenametextbox.text.其他所有东西似乎都可以得到它,但是后台工作人员根本没有得到任何价值.
我创建了一个mesgbox,以在后台工作程序启动且其为空时显示计算机名称.

我知道后台工作人员的工作是因为我将strComputer更改为可以正常运行的机器名称.

I have a textbox on my mainform that i put a remote machine name. I have a madule taht i declared public strcomputer as string = machinenametextbox.text. Everything else seems to get it but the background workers dont get the value at all.
i have created a mesgbox to display the machine name when the background worker starts and its empty.

I know the background workers work becasue if i change strComputer in them to a machine name it runs fine.

how can i declare a variable that my background workers get?

推荐答案

您不能与backgroundworker在视图中进行任何交互,因为这全部在UI线程上,并且后台工作在另一个线程上.如果希望获得该值,请为文本框提供一个事件处理程序,并在发生更改时更新变量(strComputer).如果您想从后台工作人员更新它,我将使用ReportProgress方法(公共void ReportProgress(int percentProgress,object userState);)将其传递.另请注意,启动后台工作程序时可以传递状态信息(公共void RunWorkerAsync(object arguments);).
You cannot interact with anything in the view with a backgroundworker because that is all on the UI thread, and the backgroundworking is on another thread. If you want to be able to the value, have an event handler for the textbox, and update a variable when there is a change (strComputer). If you want to update this from the background worker I would pass it with the ReportProgress method (public void ReportProgress(int percentProgress, object userState);). Also note that you can pass state inforamtion in when you start the backgroundworker (public void RunWorkerAsync(object argument);)


.NET中没有全局变量"的概念(松一口气!无需解释为什么不好).

您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview扫描仪和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

要控制线程之间共享的数据的访问,使用线程包装器非常方便.它也有助于避免参数化的线程启动(这是不好的,因为需要强制类型转换),因为线程主体可以是实例(非静态)方法,因此也可以是"this"引用(对包装器实例的引用) )传递给线程主体,并因此传递给所有实例成员.请查看我过去关于以下主题的答案:
如何将ref参数传递给线程 [ ^ ],
启动后更改线程(生产者)的参数 [ ^ ].

这是VSNetVbHarry很好地提供的VB.NET转换:
将参数传递给线程化的LongRunningProcess [
There is no a concept of "global variable" in .NET (what a relief! no need to explain why it''s bad).

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

To control access of data shared between thread, it''s very convenient to have a thread wrapper. It also helps to avoid parametrized thread start (which is bad because required type cast), as a thread body could be an instance (non-static) method, and hence "this" reference (which is a reference to an instance of the wrapper) is passed to the thread body, and hence all the instance members. Please see my past answers on the topic:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

This is the VB.NET translation nicely offered by VSNetVbHarry:
Passing arguments to a threaded LongRunningProcess[^].

—SA


这篇关于帮助使用vb.net声明全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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