Windows Develop Bookmark and Share   
 index > Windows Forms General > Panel.Invalidate from a callback thread.
 

Panel.Invalidate from a callback thread.

I'm trying to invalidate a Panel from a callback. This is the code I have:

Code Block

Public Class Form1

Public Delegate Sub PanelInvalidateDelegate(ByVal R As Rectangle)

Public Sub SubOnCallbackThread()

Dim PID As PanelInvalidateDelegate = AddressOf Panel2.Invalidate

PID.Invoke(New Rectangle(0, 0, 42, 256))

End Sub

End Class

The Sub "SubOnCallbackThread" executes without error but the panel never gets invalidated and paints. What am I doing wrong?

JohnWein  Wednesday, October 10, 2007 9:17 PM
I think you may be confusing two meanings of Invoke. The one you're using is the Invoke method of the delegate, which runs it on a new thread pool thread. What I think you're trying to do, however, is call Invalidate using the thread that created the panel. Instead of

PID.Invoke(New Rectangle(0, 0, 42, 256))


try doing (sorry, I don't know VB syntax -- the second argument should be an array of Object containing a single element--the rectangle)


Panel2.Invoke(PID, New Object() { New Rectangle(0, 0, 42, 256) } )


This will wait for the message loop thread to execute the method pointed to by PID (Panel2.Invalidate in this case).


If you actually did want it to run asynchronously, which is what [delegate].Invoke is used for, use Panel2.BeginInvoke instead.


This might all be stuff that I'm incorrectly bringing from C#, though...
sirjis  Wednesday, October 10, 2007 10:28 PM
I think you may be confusing two meanings of Invoke. The one you're using is the Invoke method of the delegate, which runs it on a new thread pool thread. What I think you're trying to do, however, is call Invalidate using the thread that created the panel. Instead of

PID.Invoke(New Rectangle(0, 0, 42, 256))


try doing (sorry, I don't know VB syntax -- the second argument should be an array of Object containing a single element--the rectangle)


Panel2.Invoke(PID, New Object() { New Rectangle(0, 0, 42, 256) } )


This will wait for the message loop thread to execute the method pointed to by PID (Panel2.Invalidate in this case).


If you actually did want it to run asynchronously, which is what [delegate].Invoke is used for, use Panel2.BeginInvoke instead.


This might all be stuff that I'm incorrectly bringing from C#, though...
sirjis  Wednesday, October 10, 2007 10:28 PM

sirjis: Thanks for your response. You are right I'm confused. You are also right in that I am trying to invalidate the panel using the thread that created the panel. I'll try your suggestions. I'm sure they will get me past my current impast.

JohnWein  Wednesday, October 10, 2007 10:43 PM

I chaned the invoke line to:

Code Block

Panel2.Invoke(PID, New Rectangle(0, 0, 42, 256))

and I get an error:

"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."

But that can't be the true description of the error, because the sub can't be called until after the window is shown and the window definitely has a handle.

JohnWein  Thursday, October 11, 2007 6:39 AM
There's something wrong with your panel. In your original code, you should have gotten an IllegalOperationException if the panel had a handle. If you created it dynamically, be sure to add it to the form's Controls collection. You don't have to use the panel's Invoke() method, Me.Invoke() will work too.
nobugz  Thursday, October 11, 2007 11:46 AM

I've got more problems than Panel.Invalidate. I have a C implementation of a TV Automatic Volume Control that I'm trying to transfer to VB.Net. Some things are easier to do in native C than they are in .Net. It's a learning experience. Now I'm hung up on a Wave call and this isn't the forum for that.

sirjis:

Thanks for pointing me in the right direction for invoke. I have a better feeling for delegates now.

JohnWein  Thursday, October 11, 2007 2:21 PM

You can use google to search for other answers

Custom Search

More Threads

• is there a C# equivalent of the isnumeric() function of VB?
• Accessing information in a form TextBox when the TextBox was created at Run Time
• Cascading child forms location reset
• Displaying selected items in an unfocused ListView
• checking status of keys and mouse clicks
• replace windows desktop with my application
• The Form events are not caught
• fighting with framework 1.1 install
• Windows Forms UserControl OnPaint Exception
• Change ListView Check behavior so items don't get checked when multiple items are being highlighted.