在类中而不是在表单中使用invoke

时间:2016-12-09 21:10:06

标签: vb.net

我写了一个程序,读取一个信号的COM端口,一切正常,但他们想让应用程序成为一个服务,所以我将应用程序类型交换为'Windows服务'并创建一个类,并将所有内容放在窗体中在那里,我在启动模块的Main()中调用了类。这条线,

   Me.Invoke(New myDelegate(AddressOf UPdateVariable), New Object() {})
在课堂上的

用红色调用说“'调用,不是Moisture.Moisture的成员。'”并且该行的“我”部分不再像表格中那样变灰。它起作用之前不知道是什么造成了不同。

这是该类的整个代码

Imports System
Imports System.IO.Ports
Imports System.Net.Mime

Public Class Moisture
    Dim WithEvents serialPort As New IO.Ports.SerialPort
Public Delegate Sub myDelegate()
Public RawString As New System.Text.StringBuilder
Public value As String

Public Sub StartListening()
    If serialPort.IsOpen Then
        serialPort.Close()
    End If

    Try
        With serialPort
            .PortName = "COM3"
            .BaudRate = 9600
            .Parity = Parity.None
            .StopBits = StopBits.One
            .DataBits = 8
            .Handshake = Handshake.None
            .RtsEnable = True
        End With
        serialPort.Open()
    Catch ex As Exception

    End Try
End Sub

 Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
    Me.Invoke(New myDelegate(AddressOf UPdateVariable), New Object() {})

End Sub

Public Sub UPdateVariable()
    With RawString
        .Append(serialPort.ReadLine())
    End With
    If RawString.ToString().Count(Function(x As Char) x = "%"c) = 2 Then
        PiTagUpdater(StringParser(RawString.ToString()))
        RawString.Clear()
    End If
End Sub

Public Function StringParser(RawString As String) As String
    Dim Moisture = RawString
    Dim value As String
    Dim values As String() = Moisture.Split(New Char() {":"c})
    value = values(values.Length - 1).Trim({" "c, "%"c})
    Return value
End Function

Private Sub PiTagUpdater(Value As Decimal)


    Try
        Dim piserver As New PCA.Core.PI.PIServer(PCA.Core.Globals.Applications.Application("GENERAL").ConfigValues.ConfigValue("PI_SERVER_NAME").StringValue, PCA.Core.Globals.Applications.Application("GENERAL").ConfigValues.ConfigValue("PI_SERVER_UID").GetDeCryptedStringValue, PCA.Core.Globals.Applications.Application("GENERAL").ConfigValues.ConfigValue("PI_SERVER_PASSWD").GetDeCryptedStringValue, True)
        Dim TimeStamp As DateTime = FormatDateTime(Now)
        Dim RapidRingCrush = "M1:RapidRingCrush.T"
        Try
            piserver.WriteValue(RapidRingCrush, Value, TimeStamp)
        Catch ex As Exception
            MessageBox.Show("Error occured locating Pi Tag", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()
        End Try

    Catch ex As Exception
        MessageBox.Show("Cannot connect to Pi Server")
    End Try

    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

Me指的是当前对象,在本例中是您的类的实例。您的类没有Invoke()方法(如错误所示),但是System.Windows.Forms.Control派生的任何内容都有(例如Form)。

Control.Invoke()用于将方法的执行移动到创建控件的同一线程。这用于实现线程安全。

由于您切换到服务,因此可以假设您没有用户界面,因此无需调用。删除Invoke()调用并手动调用该方法就足够了:

Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
    UPdateVariable()
End Sub

修改

正如LarsTech所说,您还必须将消息框切换为某种记录方法。 Windows服务通常在不同的帐户下运行,这意味着它无法向当前用户显示用户界面。