Article View: be.comp.programming
Article #3143VB.NET: thread error
From: Snuyt
Date: Thu, 05 Feb 2004 18:23
Date: Thu, 05 Feb 2004 18:23
88 lines
3614 bytes
3614 bytes
Hallo,
Ik heb een client-serverprogrammatje. Hieronder staat een uittreksel
(vereenvoudigd) van mijn clientcode. In de sub DRAW_AN_IMAGE probeer ik
een picturebox toe te voegen aan de Form, maar dat lukt niet, hij geeft
als error:
"Controls created on one thread cannot be parented to a control on a
different thread."
op de lijn : Me.Controls.Add(p)
Weet er iemand wat ik hieraan kan doen?
Private Sub form_Load(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles MyBase.Load
Try
client = New TcpClient(ip, port)
client.GetStream.BeginRead(readBuffer,0,READ_BUFFER_SIZE, _
AddressOf DoRead, Nothing)
Catch Ex As Exception
MsgBox("Cannot Connect", MsgBoxStyle.Exclamation, "Error")
Me.Close()
End Try
End Sub
Private Sub DoRead(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String
Try
BytesRead = client.GetStream.EndRead(ar)
If BytesRead < 1 Then
MarkAsDisconnected("No butes read")
Exit Sub
End If
' Convert the byte array the message was saved into, minus
' two for the Chr(13) and Chr(10)
strMessage = Encoding.ASCII.GetString(readBuffer,0,_
BytesRead - 2)
Catch e As Exception
MarkAsDisconnected(e.Message)
Exit Sub
End Try
ProcessCommands(strMessage)
Try
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE,_
AddressOf DoRead, Nothing)
Catch e As Exception
MarkAsDisconnected(e.Message)
End Try
End Sub
' Process the command received from the server,
' and take appropriate action.
Private Sub ProcessCommands(ByVal strMessage As String)
Dim dataArray() As String
' Message parts are divided by "|" Break the string
' into an array accordingly.
dataArray = strMessage.Split(Chr(124))
' dataArray(0) is the command.
Select Case dataArray(0)
Case "IMG"
DRAW_AN_IMAGE(dataArray(1))
Case Else
End Select
End Sub
Private Sub DRAW_AN_IMAGE(ByVal data As String)
Dim p As New PictureBox()
p.Image = New Bitmap("pics/" + data + ".bmp")
p.SetBounds(1, 1, 72, 96)
Me.Controls.Add(p)
p.BringToFront()
End Sub
Bedankt,
Snuyt
Technical Details
Message-ID:
<40228a21$0$310$ba620e4c@news.skynet.be>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!skynet.be!skynet.be!louie!tjb!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 107324,27c8cf66f3bc38b5,start X-Google-Attributes: gid107324,public X-Google-Thread: fcea4,27c8cf66f3bc38b5,start X-Google-Attributes: gidfcea4,public X-Google-ArrivalTime: 2004-02-05 10:23:30 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!skynet.be!skynet.be!louie!tjb!not-for-mail Date: Thu, 05 Feb 2004 19:23:46 +0100 From: Snuyt <snuyt_nospam@skynet.be> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5) Gecko/20031007 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: be.comp.programming,nl.comp.programmeren Subject: VB.NET: thread error Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Lines: 89 Message-ID: <40228a21$0$310$ba620e4c@news.skynet.be> Organization: -= Belgacom Usenet Service =- NNTP-Posting-Host: 217.136.130.6 X-Trace: 1076005410 reader0.news.skynet.be 310 217.136.130.6:10494 X-Complaints-To: usenet-abuse@skynet.be