3

我有一个从 USB 徽章阅读器获取用户 ID 的功能,用于登录应用程序。当我运行该应用程序时,在我滑动标签之前不会出现登录窗口。我需要知道是否可以加载 Windows,然后开始运行从 USB 获取数据的功能。

谢谢 :)

Private Sub SerialPort1_DataReceived()
    'Threading.Thread.SpinWait(1000)
    OpenPort()
    If SerialPort1.IsOpen() Then

        byteEnd = SerialPort1.NewLine.ToCharArray
        'read entire string until .Newline 
        readBuffer = SerialPort1.ReadLine()
        readBuffer = readBuffer.Remove(0, 1)
        readBuffer = readBuffer.Remove(8, 1)
        WWIDTextBox.AppendText(readBuffer)

    End If
End Sub

Private Sub Form1_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
    SerialPort1_DataReceived()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'SerialPort1_DataReceived()
End Sub
4

4 回答 4

6

问题是您正在调用该ReadLine方法,这是一个阻塞(同步)方法。换句话说,当你调用它时,该方法在它有要返回的值之前不会返回值。因此,它会停止在当前线程上的执行,直到读取完整的行(当刷卡时)。由于您在调用它时处于 UI 线程上,因此它会锁定 UI,直到刷过徽章为止。

SerialPort1_DataReceived您可以从不同的线程执行工作,而不是从 UI 线程调用您的方法。最简单的方法是BackgroundWorker在设计器中将组件拖到表单上。然后你可以添加这样的代码:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    OpenPort()
    If SerialPort1.IsOpen() Then
        byteEnd = SerialPort1.NewLine.ToCharArray
        Dim readBuffer As String = SerialPort1.ReadLine()
        readBuffer = readBuffer.Remove(0, 1)
        readBuffer = readBuffer.Remove(8, 1)
        e.Result = readBuffer
    End If
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    WWIDTextBox.AppendText(CStr(e.Result))
End Sub
于 2013-04-12T15:41:17.427 回答
1

在 VS2013 上工作时,我遇到了同样的问题,我需要刷新 datagridview(网格中的颜色)。这对我有用。

Sub MyForm_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged

            If Me.Visible Then
                'do action...
            End If

End Sub
于 2016-01-04T22:34:22.360 回答
0
Private Sub loadCombo()
    Dim sqlconn As New OleDb.OleDbConnection
    Dim connString As String
    connString = ""
    Dim access As String
    access = "select slno from atable"
    Dim DataTab As New DataTable
    Dim DataAdap As New OleDbDataAdapter(access, connString)
    DataAdap.Fill(DataTab)
    ComboBox1.DataSource = DataTab
    ComboBox1.DisplayMember = "slno"

End Sub
于 2014-12-17T05:05:45.957 回答
0

尝试表单激活事件

Private Sub Form1_Activated(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Activated
 'Call your function here
End Sub 

它调用函数After the Form Loads...

于 2013-04-12T15:18:35.180 回答