Windows Develop Bookmark and Share   
 index > Windows Forms Designer > InvokePaintbBackground method
 

InvokePaintbBackground method

Hi
I use this method to replace the background of a panel from another thread but I get this error message:

Cross-thread operation not valid:

Dim arg As New PaintEventArgs(mypanelgraphics, Bounds)
If Me.InvokeRequired Then
InvokePaintBackground(Me, arg)
Else
Me.Refresh()
End If

If I use InvokePaint(me, arg) I do not get the error but it does not refresh mypanel because I paint to a bitmap and then set it as the panel background picture.

Any ideas how to sole this ?

Dewald Viljoen  Monday, January 19, 2009 1:28 AM
Hi Dewald Viljoen,

The control can only be accessed on which thread its handle is created. So when you acess the control in a thread other than UI thread, it is possible thread safety problems occured. As a result, a "cross-thread operation not valid" exception was throw.

There are four methods on a control are thread safe Invoke, BeginInvoke, EndInvoke, and CreateGraphic. You can call these methods on any threads. Please refer to MSDN document for more details.

I am not sure if you are passing the wrong reference in the InvokePaint method since you are going to paint the panel. However, you are passing the reference of the Form.

Of cousre, I also wrote the code how to use InvokePaint to update the panel in another thread. The following are the code, hope you can get some hints from it.

*PrivateSubbutton2_Click(ByValsenderAsObject,ByValeAsEventArgs)
*DimtAsNewThread(NewThreadStart(SetBackGround))
*t.Start()
*EndSub
*PrivateSubSetBackGround()
*DimgAsGraphics=Me.CreateGraphics()
*DimeAsNewPaintEventArgs(g,Me.panel1.ClientRectangle)
*g.FillRectangle(Brushes.Red,Me.panel1.ClientRectangle)
*IfInvokeRequiredThen
*InvokePaint(Me.panel1,e)
*EndIf
*g.Dispose()
*EndSub

If you have any further problem, please don't hesitate to let me know.

Best regards,
Bruce Zhou

Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Tuesday, January 20, 2009 4:00 AM
Thank you I appreciate it.
Dewald Viljoen  Thursday, January 22, 2009 4:57 PM
Hi Dewald Viljoen,

The control can only be accessed on which thread its handle is created. So when you acess the control in a thread other than UI thread, it is possible thread safety problems occured. As a result, a "cross-thread operation not valid" exception was throw.

There are four methods on a control are thread safe Invoke, BeginInvoke, EndInvoke, and CreateGraphic. You can call these methods on any threads. Please refer to MSDN document for more details.

I am not sure if you are passing the wrong reference in the InvokePaint method since you are going to paint the panel. However, you are passing the reference of the Form.

Of cousre, I also wrote the code how to use InvokePaint to update the panel in another thread. The following are the code, hope you can get some hints from it.

*PrivateSubbutton2_Click(ByValsenderAsObject,ByValeAsEventArgs)
*DimtAsNewThread(NewThreadStart(SetBackGround))
*t.Start()
*EndSub
*PrivateSubSetBackGround()
*DimgAsGraphics=Me.CreateGraphics()
*DimeAsNewPaintEventArgs(g,Me.panel1.ClientRectangle)
*g.FillRectangle(Brushes.Red,Me.panel1.ClientRectangle)
*IfInvokeRequiredThen
*InvokePaint(Me.panel1,e)
*EndIf
*g.Dispose()
*EndSub

If you have any further problem, please don't hesitate to let me know.

Best regards,
Bruce Zhou

Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Tuesday, January 20, 2009 4:00 AM
Thank you I appreciate it.
Dewald Viljoen  Thursday, January 22, 2009 4:57 PM

You can use google to search for other answers

Custom Search

More Threads

• vb.net 2005 shutdown error when adding default icon to property page
• Probem in Loading the Form at Design time
• Hooking Keyboard events from the DesignSurface
• need some help
• Serialization logic for a custom XML serializer
• What is Regular Expression for US Phone Number(Windows application)?
• Difference between DrawGrid, ShowGrid
• Control Name property
• Binding TextBox's Text property to application settings and setting UseSystemPasswordChar to true
• Windows application with HtML Templates