我有一个从USB徽章阅读器获取用户ID的功能,用于登录应用程序。 当我运行应用程序时,直到我滑动标记才会出现登录窗口。 我需要知道是否可以加载窗口,然后开始运行从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
答案 0 :(得分:6)
问题是您正在调用ReadLine
方法,这是一种阻塞(同步)方法。换句话说,当您调用它时,该方法不会返回该值,直到它具有要返回的值。因此,它会在当前线程上停止执行,直到读取完整的行(刷卡时)。由于您在调用它时处于UI线程,因此它会锁定UI,直到刷过徽章。
您可以从其他线程执行工作,而不是从UI线程调用SerialPort1_DataReceived
方法。最简单的方法是将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
答案 1 :(得分:1)
在VS2013上工作时,我遇到了同样的问题,我需要进行datagridview刷新(gridrows中的颜色)。这对我有用。
Sub MyForm_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Me.Visible Then
'do action...
End If
End Sub
答案 2 :(得分:0)
尝试Form Activated活动
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
...
答案 3 :(得分: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