Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)

C#
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:

C#
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
C#
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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900