vb.net threading.thread传递变量的地址 [英] vb.net threading.thread addressof passing variables
问题描述
我正在尝试将某些状态从共享子项中的IP呼叫传递到表单上的标签.
I am trying to pass some status from a IP call in a shared sub to a label on my form.
这是我正在使用的当前代码:
This is the current code that i am using:
Class Server
<STAThread()> Public Shared Sub Main()
Dim aTcpMessaging As IMessagingSystemFactory = New TcpMessagingSystemFactory()
Dim anInputChannel As IInputChannel = aTcpMessaging.CreateInputChannel(theIPforLocal & ":" & thePort)
Dim aStringMessagesFactory As IStringMessagesFactory = New StringMessagesFactory()
Dim aStringMessageReceiver As IStringMessageReceiver = aStringMessagesFactory.CreateStringMessageReceiver()
AddHandler aStringMessageReceiver.MessageReceived, AddressOf StringMessageReceived
aStringMessageReceiver.AttachInputChannel(anInputChannel)
End Sub
Private Shared Sub StringMessageReceived(ByVal sender As Object, ByVal e As StringMessageEventArgs)
LANResponse = Convert.ToString(e.Message)
Dim lanSent As String() = Nothing
Dim sep(3) As Char
Dim s As String = ""
sep(0) = "~"
'sep(1) = ","
lanSent = LANResponse.Split(sep, 2)
Dim a As New Threading.Thread(AddressOf getStatus)
a.SetApartmentState(Threading.ApartmentState.STA)
a.Start(Trim(lanSent(0)) & Trim(lanSent(1)))
End Sub
End Class
Private Shared Sub getStatus(ByVal data As Object)
lblStatus.text = ("Static: " & data)
End Sub
它给我的错误在行上 lblStatus.text =("Static:"& data)
错误1如果没有该类的显式实例,则无法从共享方法或共享成员初始化程序中引用该类的实例成员.
Error 1 Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
任何帮助都会很棒!
大卫
更新
添加后:
Private Sub getStatus(ByVal data As Object)
If InvokeRequired Then
Invoke(New Action(Of Object)(AddressOf getStatus), data)
Else
lblStatus.Text = ("Static: " & data)
End If
End Sub
我没有一行得到此错误将新的Threading.Thread(AddressOf getStatus)昏暗
I get this error no one line Dim a As New Threading.Thread(AddressOf getStatus)
错误1重载解析失败,因为无法使用以下参数调用可访问的新建": 'Public Sub New(以System.Threading.ParameterizedThreadStart开头):对非共享成员的引用需要一个对象引用. 'Public Sub New(从System.Threading.ThreadStart开始):方法'Private Sub getStatus(作为对象的数据)'不具有与委托'Delegate Sub ThreadStart()'兼容的签名.
Error 1 Overload resolution failed because no accessible 'New' can be called with these arguments: 'Public Sub New(start As System.Threading.ParameterizedThreadStart)': Reference to a non-shared member requires an object reference. 'Public Sub New(start As System.Threading.ThreadStart)': Method 'Private Sub getStatus(data As Object)' does not have a signature compatible with delegate 'Delegate Sub ThreadStart()'.
推荐答案
我注意到您正在尝试使用Shared
成员来完成这项工作.这可能是有问题的,原因如下:
I've noticed you're trying to make this work using Shared
members. That may be problematic for the following reasons:
(1)在没有Shared
的情况下,在后台创建了一个引用变量,该变量称为Me
.通过Me
,您可以进入lblStatus
,例如Me.lblStatus
,实际上不必一定要放入Me
,一个人可以自己指定lblStatus
.但是,即使单独使用Me
,它也隐含在其中,并且仍在使用.
(1) Without the Shared
, behind the scenes a reference variable is created, and it is called Me
. Via Me
you can get to lblStatus
, such as Me.lblStatus
, and actually one doesn't necessarily need to put in the Me
, one could specify lblStatus
by itself. But, even by itself, the Me
is implied there, and is still used.
(2)对于Shared
,没有Me
.没有Me
,编译器就没有lblStatus
实例可以识别-因此出错.
您将需要找到某种方式来传递对您的表格的引用,以从中访问lblStatus
.但是,我需要警告您,直接从除主线程之外的其他线程更新Label或其他用户界面类被认为是不好的-这是因为WinForms并不是线程安全的.从.NET Framework 2开始,如果尝试尝试将收到错误消息(尽管有可能自行退出此错误,但后果自负).
(2) With the Shared
, there is no Me
. Without the Me
, there is no lblStatus
instance for the compiler to identify - hence the error.
You will need to find some way to pass in a reference to your Form from which to access lblStatus
. I need to warn you, though, that updating a Label or other user interface class directly from a thread other than the main thread is regarded as something bad - this is because WinForms is not thread-safe. Beginning with .NET Framework 2, you'll get an error if you try (although it is possible to opt-out of this error, at your own peril).
这些用于C#,但可能会为您提供一些有关如何进行的想法:
These are for C#, but it may give you some ideas on how to proceed:
http://www.codeproject.com/Articles/23517/How-to-Properly-Handle-Cross-thread-Events-and-Upd
所以第一步是使getStatus不是共享成员-这将带回Me
.
So step one is to make getStatus not a shared member - that will bring back the Me
.
第二步是将getStatus重新设计为线程安全的形式,以将文本获取到Label中,例如:
Step two is to redesign getStatus into a thread-safe form for getting the text into the Label, such as:
Private Sub getStatus(ByVal data As Object)
If InvokeRequired Then
Invoke(New Action(Of Object)(AddressOf getStatus), data)
Else
lblStatus.Text = ("Static: " & data)
End If
End Sub
第三步是找到某种方法来获取对您的Form的引用到Server
中-可能是通过共享的ArrayList?还是只是共享引用了Form?
Step three is to find some way to get a reference to your Form into Server
- may be via a shared ArrayList? Or just some shared reference to Form?
第四步是对StringMessageReceived的重新设计,以使AddressOf
部分现在采用AddressOf refToMyForm.getStatus
的形式.
Step four is a redesign of StringMessageReceived so that the AddressOf
portion is now in the form of AddressOf refToMyForm.getStatus
.
这篇关于vb.net threading.thread传递变量的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!