I have a Picturebox on my main form that I am trying to refresh as I change/process its image in a different background thread. I originally tried to do it by calling refresh directly, but it complained telling me that I can't do it that because it is an unsafe cross-threading operation. So I set up the Delegate Callbacks, like so:
DelegateSub SetPicRefreshCallback()
PrivateSub SetPic()IfMe.PicBoxAGDDisp.InvokeRequired ThenDim d AsNew SetPicRefreshCallback(AddressOf SetPic)Me.Invoke(d)ElseTryMe.PicBoxAGDDisp.Refresh()Catch ex As Exception
Thread.Sleep(0)
EndTryEndIfEndSub
So now I call SetPic when I want to refresh it. I put the try/catch and thread.sleep in there so I could break on an exception and see what was causing my headaches. It worked for a while, but now, for some reason, it works only the first time, and the very next time calling SetPic, I get an exception:
System.ArgumentException was caught
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Image.get_RawFormat()
at System.Drawing.Graphics.IgnoreMetafileErrors(Image image, Int32& errorStatus)
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.UpdateWindow(HandleRef hWnd)
at System.Windows.Forms.Control.Update()
at System.Windows.Forms.Control.Refresh()
at AGD_Emulator.Form1.SetPic() in C:\Documents and Settings\chris.secules\My Documents\TAD Emulators\Working\AGD2005\AGD_Emulator\Form1.vb:line 537
Any ideas what might be causing this exception? Please help...I'm desperate!
Thanks,