lbxUsbFile.Invoke(Sub() lbxUsbFiles.Items.AddRange(My.Computer.FileSystem.GetFiles(UsbLowroyDir).ToArray()))
lbxUsbFile.Invoke(Sub() lbxUsbFiles.Items.AddRange(My.Computer.FileSystem.GetFiles(UsbLowroyDir).ToArray()))
Private Sub GetUSBDriveLetter()
' Get Drive Letter
lbxUSBFiles.Invoke(Sub() lbxUSBFiles.Items.Clear())
Dim USBLowreyDir As String = ""
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim drvLetter As String = ""
'Dim d As DriveInfo
For Each drvInfo In allDrives
If drvInfo.DriveType = 2 Then
drvLetter = drvInfo.Name
USBLowreyDir = drvLetter & "Lowrey"
Exit For
End If
Next
If drvLetter <> Nothing Then
If Not (My.Computer.FileSystem.DirectoryExists(USBLowreyDir)) Then
Dim mbText, mbTitle As String, mbResp As Integer
Dim mbBtns As MsgBoxStyle
mbTitle = "NO LOWREY FOLDER"
mbBtns = CType(vbInformation + vbOKOnly, MsgBoxStyle)
mbText = "There is no LOWREY folder on this thumbdrive."
mbResp = MsgBox(mbText, mbBtns, mbTitle)
Exit Sub
Else
lbxUSBFiles.Invoke(Sub() lbxUSBFiles.Items.AddRange(My.Computer.FileSystem.GetFiles(USBLowreyDir, FileIO.SearchOption.SearchAllSubDirectories, FileType).ToArray()))
End If
End If
End Sub
Elapsed
event of a System.Timers.Timer
. Private Sub StartDetection()
Dim query2 As New WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 " & "WHERE TargetInstance ISA 'Win32_DiskDrive'")
MediaConnectWatcher = New ManagementEventWatcher With {
.Query = query2
}
MediaConnectWatcher.Options.Timeout = New TimeSpan(1000)
MediaConnectWatcher.Start()
End Sub
Private Sub Arrived(ByVal Sender As Object, ByVal E As System.Management.EventArrivedEventArgs) Handles MediaConnectWatcher.EventArrived
Dim USBLowreyDir As String = ""
Dim mbo, obj As ManagementBaseObject
mbo = CType(E.NewEvent, ManagementBaseObject)
obj = CType(mbo("TargetInstance"), ManagementBaseObject)
Select Case mbo.ClassPath.ClassName
Case "__InstanceCreationEvent"
GetUSBDriveLetter()
Case "__InstanceDeletionEvent"
If obj("InterfaceType").ToString.ToUpper = "USB" Then
lbxUSBFiles.Invoke(Sub() lbxUSBFiles.Items.Clear())
End If
End Select
SerialPort.DataArrived
event. The FileSystemwatcher
class raises its events on a secondary thread too, for the same reason. It appears that the ManagementEventWatcher.EventArrived
event can be added to that list. That event can be raised at any time and they don't want to interrupt what the user is doing in the UI at the time so the event is raised on a secondary thread, meaning that the event handler is executed on that same secondary thread. If you want to affect the UI, you need to marshal a call to the UI thread to do so.