newbie threding problem

okpixel

Newcomer
Joined
Jan 14, 2005
Messages
1
I have created my first winform, and as is here, it works fine....

My problem comes when the tread completes. I can display a msgBox telling me that the thread has completed, but i cant set the text of a textbox :(

Can you tell me why that is :confused:

Imports System.Net
Imports System.Threading
Imports System.Web

Public Class Form1

Public WithEvents osomejobobject As somejobobject

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

osomejobobject = New somejobobject()
osomejobobject.url = TextBox2.Text
TextBox1.Text += "starting thread...(" & osomejobobject.url & ")" & vbNewLine
Dim t As Thread = New Thread(AddressOf osomejobobject.somejob)
t.Start()
End Sub

Sub ThreadCompleteEventHandler(ByVal urllog As String) Handles osomejobobject.ThreadComplete
'****************************************
'*********** :( *************
'****************************************
'I WOULD LIKE >
' TextBox1.Text += urllog & vbNewLine
'*****************************************
MsgBox(urllog & vbNewLine)

End Sub

End Class




Public Class somejobobject
Public url As String ' in
Public urllog As String ' out
Public Event ThreadComplete(ByVal urllog As String)

Public Sub somejob()
Dim wresponse As HttpWebResponse
Dim wrequest As HttpWebRequest
Try
wrequest = CType(WebRequest.Create(url), HttpWebRequest)
wrequest.UserAgent = "som UA"
wrequest.Referer = "http://someref.com"
wrequest.Timeout = 10000
wresponse = CType(wrequest.GetResponse(), HttpWebResponse)
wresponse.Close()
SyncLock GetType(somejobobject)
urllog = "ResponseUri:" & wresponse.ResponseUri.ToString() & vbNewLine & _
"contenttype: " & wresponse.ContentType & vbNewLine & _
"contentlength: " & wresponse.ContentLength & " bytes" & vbNewLine & _
"------------------------------------------------"
End SyncLock
Catch ex As Exception
SyncLock GetType(somejobobject)
urllog = "nothing came back" & vbNewLine & _
"------------------------------------------------"
End SyncLock
End Try
RaiseEvent ThreadComplete(urllog)
End Sub


End Class
 
Well, I think you can only access a control from the thread that it was created with. So perhaps you can stall the other thread in the button click sub to wait until the thread t finishes and then put the information in variables so that the original thread (not t) can put the values into the textbox. :)
 
Back
Top