Ok, this is probably an easy one and I have made a right pigs ear of it, so please bare with me as threading is new to me, but I am trying to understand from my mistakes.
I have a main form, that calls a new thread in a SUB located in a module.
While the sub is processing (scanning files), it is supposed to update a text box in another form (that I check if it is opened via a global boolean declaration if it is true or not. If it is true (i.e. the form is open) then it will call a settext sub (example taken from a MS example, which is a simplified example of others I have seen on the web).
So the code is like this:
Detailedlog form (contains one textbox, multi-line, scrollbars-vertical)
Public Class DetailedLog
Delegate Sub SetTextCallback([text] As String)
Private Sub DetailedLog_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
ShowingDetailedLog = False
Me.Dispose()
End Sub
Private Sub DetailedLog_Shown(sender As Object, e As EventArgs) Handles Me.Shown
ShowingDetailedLog = True
End Sub
Public Sub SetText(ByVal [text] As String)
Try
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.TextBox1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.TextBox1.AppendText([text])
End If
Catch ex As Exception
MessageBox.Show(ex.Message, AppErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
'Return
End Try
End Sub
Then in my thread code that has been called from my main form, the thread code is as so:
Dim fileEntries As String() = IO.Directory.GetFiles(TargetDirectory, WildCards)
' Process the list of files found in the directory.
For z = 0 To fileEntries.Count - 1
If ShowingDetailedLog = True Then
DetailedLog.SetText("Checking File:" & fileEntries(z) & vbNewLine)
End If
Next
My InvokeRequired code always returns as false, even with the form open. When I do "force" it via debug and step into the true section of the code, it falls over with
Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
Any help on this would be most appreciated, driving me crazy! Thanks! :)
What I have tried:
I have done various searches and tried examples and although the examples work, I am unable to implement it into my project.