Hi techman,
Visual Basic:
_Djinni = New Threading.Thread(AddressOf Monitor)
_Djinni.IsBackground = True
_Djinni.Start()
This is the start of the threading op. It's defined earlier in the code as a class level variable (for reference by the other methods and properties within).
Visual Basic:
Private Sub Monitor()
'Set the starting state of the old connection...
Dim oldConnect As PDAConnectState = PDAConnectState.NotPresent
Dim oldFiles() As String = Nothing
Dim newFiles() As String = Nothing
'Flags to make sure that certain events are only raised once...
Dim flagDisconnect As Boolean = False
Dim flagTimeout As Boolean = False
Dim flagWaitfail As Boolean = False
'Only run the monitor loop while the class is not disposed and the thread is running...
Do While Not isDisposed And DjinniState <> (Threading.ThreadState.Stopped Or Threading.ThreadState.StopRequested)
'Check the current state of the PDA...
_ConnectState = PDA.IsConnected()
'Respond appropriately to the connection state...
Select Case _ConnectState
Case PDAConnectState.Connected
'Set the flags...
flagDisconnect = False
flagWaitfail = False
flagTimeout = False
'Check to see if this is a new connection...
If _ConnectState <> oldConnect Then
_ActivePDA = New PDA
_ActivePDA.GetPDADetail()
ClearShare()
RaiseEvent PDAConnected()
'We can wait until the next time round the loop to receive the connection file.
Else
'Check to see if the target path actually exists - if it doesn't then create it...
If Not IO.Directory.Exists(_ActivePDA.PdaPcFileSharePath) Then
IO.Directory.CreateDirectory(_ActivePDA.PdaPcFileSharePath)
End If
'Check to see if a file's been dropped...
newFiles = IO.Directory.GetFiles(_ActivePDA.PdaPcFileSharePath)
Array.Sort(newFiles)
If Not oldFiles Is Nothing Then
Array.Sort(oldFiles)
End If
'Loop through and see if they're different...
For loopCounter As Int32 = 0 To newFiles.GetUpperBound(0)
Dim newIndex As Int32 = Array.BinarySearch(oldFiles, newFiles(loopCounter))
If newIndex < 0 Then
'This is a new file, so let's process it...
ProcessFile(newFiles(newIndex))
End If
Next loopCounter
End If
Case PDAConnectState.NotPresent
'Raise that the event has been raised...
If Not flagDisconnect Then
RaiseEvent PDADisconnected()
flagDisconnect = True
End If
Case PDAConnectState.TimeOut
'Note that the event has been raised...
If Not flagTimeout Then
RaiseEvent PDATimedOut()
flagTimeout = True
End If
Case PDAConnectState.WaitFail
'Note that the event has been raised...
If Not flagWaitfail Then
RaiseEvent PDAWaitFailed()
flagWaitfail = True
End If
End Select
oldConnect = _ConnectState
'Go back to sleep for a while...
Threading.Thread.Sleep(_TickDelay)
Loop
End Sub
This is the main body of the process - it simply monitors a folder to see if files are being dumped into it. I know that I can use a FileMonitor here, but I've written it like this for ease of portability between the PDA and PC.
Visual Basic:
'PDAConnected() and PDADisconnected - PDA docking state has changed
Private Sub pdaMon_PDAConnectState() Handles pdaMon.PDADisconnected, pdaMon.PDAConnected
If Me.InvokeRequired Then
[color=red]Util.NOP()[/color]
End If
'We only want to do this if the PDA monitor has been initialised properly...
If Not pdaMon Is Nothing AndAlso Not [color=red]pdaMon.IsInitialising[/color] Then
'Notify the user of the state change...
Dim resourceIcon As Int32 = 0
If pdaMon.ConnectState = PDAConnectState.Connected Then
resourceIcon = iconPdaOn
Else
resourceIcon = iconPdaOff
End If
'Display the state in the status bar at the bottom of the screen...
stpnlPda.Icon = Icon.FromHandle(DirectCast(icons16.Images(resourceIcon), Bitmap).GetHicon)
End If
End Sub
This is the final part of the jigsaw.
It all works fine - this code runs through and raises all of the events as it should. The problem seems to be when a PDA is plugged in or removed - it's supposed to display an icon in the status bar on the main form, but doesn't!!!
The PDA monitoring class is instantiated properly within the MDI form that employs its use, and is defined WithEvents. All handlers are defined properly, but, despite the fact that the events are raised by the Djinni code, the handlers do not appear to work. The odd thing, however, is that some of the routines used within the handler appear to fire (those highlighted in red)... (?!!??!?!)
I've already made the change that you've suggested, but it hasn't appeared to alter the event situation.
Any light that you can shine on this would be extrmemely appreciated.
Paul.