|
-
Dec 1st, 2007, 12:46 PM
#1
Thread Starter
Member
[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.
-
Dec 1st, 2007, 01:08 PM
#2
Frenzied Member
Re: [2005] New Thread interact with Form
Search for jmcilhinney and invokerequired
-
Dec 1st, 2007, 01:59 PM
#3
Thread Starter
Member
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:
Sub createhoneypots()
Dim honeylisten As New System.Threading.Thread(AddressOf Module1.honeylisten)
honeypot = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 2999)
' Form1.txthresults.Text = Form1.txthresults.Text & "Honeypot Succesfully Created on port: 2999" & vbNewLine
honeylisten.Start()
End Sub
Sub honeylisten()
MsgBox("")
Try
honeypot.Start()
txtresults("tdf")
While 1
If honeypot.Pending() = True Then
honeypot.AcceptTcpClient()
MsgBox(honeypot.Server.ToString())
Exit While
End If
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Delegate Sub txtdelegate(ByVal s As String)
Sub txtresults(ByVal s As String)
MsgBox("")
If Form1.txthresults.InvokeRequired Then
Form1.txthresults.Invoke(New txtdelegate(AddressOf txtresults), s)
Else
Form1.txthresults.Text = s
End If
End Sub
However this does nothing but show the message boxes. Could the problem be that all of this code is on a module?
-
Dec 1st, 2007, 08:41 PM
#4
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.
-
Dec 1st, 2007, 09:03 PM
#5
Thread Starter
Member
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?
-
Dec 1st, 2007, 09:17 PM
#6
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.
-
Dec 1st, 2007, 09:26 PM
#7
Thread Starter
Member
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.
-
Dec 1st, 2007, 09:35 PM
#8
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: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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|