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.