thread error

S

Snuyt

When I add a control to a panel, I get this error :


"Controls created on one thread cannot be parented to a control on a
different thread."


Does anyone knows what I'm doing wrong ?


Dim p As New PictureBox()
p.Image = New Bitmap("pics/" + k.getUniCode.ToString + ".bmp")
p.SetBounds(1, 2, 72, 96)
Panel.Controls.Add(p) <-- here comes the error


thx
Snuyt
 
H

Herfried K. Wagner [MVP]

* Snuyt said:
When I add a control to a panel, I get this error :

"Controls created on one thread cannot be parented to a control on a
different thread."

Does anyone knows what I'm doing wrong ?

Dim p As New PictureBox()
p.Image = New Bitmap("pics/" + k.getUniCode.ToString + ".bmp")
p.SetBounds(1, 2, 72, 96)
Panel.Controls.Add(p) <-- here comes the error

Are you using multiple threads?
 
S

Snuyt

Herfried said:
Are you using multiple threads?

It 's a client - server program. Whenever the server sends a certain
message to the client, There is a new PictureBox() created and shown. So
no multiple threading

Snuyt
 
D

Dominique Vandensteen

client - server without multithreading...
is this possible?

i think you will need to check the Invoke method on Panel...

private sub doit()
Panel.Invoke(new MethodInvoker(AddressOf InvokeThisSub))
end sub

private sub InvokeThisSub()
Dim p As New PictureBox()
p.Image = New Bitmap("pics/" + k.getUniCode.ToString + ".bmp")
p.SetBounds(1, 2, 72, 96)
Panel.Controls.Add(p)
end sub


when server "says" to add the picture, call the doit sub
 
S

Snuyt

I will create a more clearly view on my program:

CLIENT:
=======


Private Sub clientconnected_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)
Panel.Controls.Add(p)
p.BringToFront()
End Sub


The error occurs on the "Panel.Controls.Add(p)" line

Any solutions ?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top