Results 1 to 8 of 8

Thread: [2005][resolved] New Thread interact with Form

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Resolved [2005][resolved] New Thread interact with Form

    Is there anyway for a new thread to interact with my original form. For example
    Code:
    Sub createhoneypots()
            Dim honeylisten As New System.Threading.Thread(AddressOf Module1.honeylisten)
    
            honeypot = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 2999)
            honeylisten.Start()
        End Sub
    
        Sub honeylisten()
            MsgBox("")
            Try
                honeypot.Start()
                MsgBox("")
                Form1.txthresults.Text = Form1.txthresults.Text & "Honeypot Succesfully Created on port: 2999" & vbNewLine
                honeypot.AcceptTcpClient()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    That code does not write the message to the txtbox but it does show all the message boxes so i know it is getting to that code.
    Last edited by qazwsx; Dec 1st, 2007 at 10:01 PM.

  2. #2
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] New Thread interact with Form

    Search for jmcilhinney and invokerequired

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Re: [2005] New Thread interact with Form

    Ok i looked at those posts, thank you they were very helpful and i think i am getting closer. This is what i ahve so far:

    vb Code:
    1. Sub createhoneypots()
    2.         Dim honeylisten As New System.Threading.Thread(AddressOf Module1.honeylisten)
    3.  
    4.         honeypot = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 2999)
    5.         ' Form1.txthresults.Text = Form1.txthresults.Text & "Honeypot Succesfully Created on port: 2999" & vbNewLine
    6.         honeylisten.Start()
    7.     End Sub
    8.  
    9.     Sub honeylisten()
    10.         MsgBox("")
    11.         Try
    12.             honeypot.Start()
    13.             txtresults("tdf")
    14.             While 1
    15.                 If honeypot.Pending() = True Then
    16.                     honeypot.AcceptTcpClient()
    17.                     MsgBox(honeypot.Server.ToString())
    18.                     Exit While
    19.                 End If
    20.             End While
    21.         Catch ex As Exception
    22.             MsgBox(ex.Message)
    23.         End Try
    24.     End Sub
    25.  
    26.     Private Delegate Sub txtdelegate(ByVal s As String)
    27.  
    28.  
    29.     Sub txtresults(ByVal s As String)
    30.         MsgBox("")
    31.         If Form1.txthresults.InvokeRequired Then
    32.             Form1.txthresults.Invoke(New txtdelegate(AddressOf txtresults), s)
    33.         Else
    34.             Form1.txthresults.Text = s
    35.         End If
    36.     End Sub

    However this does nothing but show the message boxes. Could the problem be that all of this code is on a module?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] New Thread interact with Form

    Follow the last link in my signature for an explanation and examples.

    Display a message box directly from a worker thread is not a good idea because it creates a form modal to that thread, which means the main thread can simply ignore it. Any interaction with the UI, including displaying messages, should be performed via delegation.

    You might also want to look into using a BackgroundWorker instead of explicitly creating a Thread. There's a link in my signature to a BGW example too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Re: [2005] New Thread interact with Form

    That made things a little clearer; however, I still cannot get it to work with the thread. I removed the Msgboxes. I am jsut wondering, why are background workers preferable?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] New Thread interact with Form

    BackgroundWorkers are not preferable; they are just easier in many situations. You can use the ReportProgress method and the ProgressChanged and RunWorkerCompleted events to interact with the UI without having to invoke any delegates. All that is handled by the BGW for you. In situations where there is a lot of interaction with the UI a BGW may end up making things more complex though, so they are not always suitable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Re: [2005] New Thread interact with Form

    Hmm i cant seem to figure out why it is not working with the thread. I think my code is almost identical to the examples.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] New Thread interact with Form

    I'd first like to point out that "txtresults" is a terrible name for a method. I stringly suggest that you immediately change that to something that actually describes its purpose.

    As to your issue, in that method you're referring to your TextBox like this:
    vb Code:
    1. Form1.txthresults
    Do you have a variable in your module named "Form1" that refers to the form you want to access? I''l wager not. In that case you're referring to the default instance of the Form1 class, which is likely to not be the same instance that you've displayed.

    You first need to have a variable in your module that refers to the actual Form1 object you want to access. Then you would do something like this:
    Code:
    Private Sub SetResultsText(ByVal results As String)
        If myForm1.InvokeRequired Then
            myForm1.Invoke(New SetResultsTextCallback(AddressOf SetResultsText), results)
        Else
            myForm1.txtResults.Text = results
        End If
    End Sub
    I recommend against making your controls accessible outside their parent form too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width