PDA

View Full Version : invalid exception {"Invoke or BeginInvoke cannot be called on a control until the wi


MffM
12-11-2006, 11:43 AM
Hi all

i have a project in vb.net some times at run time this message appear

InvalidOperationException was unhandled

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


i don't know why and i didn't find any logic when it's appear because when i run my project some time its appear and some times it doesn't appear.

i read alot of articals all of them talk about thread and UI
but i didn't use thread in my project , i used alot of select statments, insert and update to the data base , for loops , show forms and show dialogs .

please how can i fix this problems and how can i know where is the problem.

this is the details of the exception

System.InvalidOperationException was unhandled
Message="Invoke or BeginInvoke cannot be called on a control until the window handle has been created."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.Control.MarshaledInvoke(Contr ol caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.BeginInvoke(Delegate method, Object[] args)
at System.Windows.Forms.Label.OnFrameChanged(Object o, EventArgs e)
at System.Drawing.ImageAnimator.ImageInfo.OnFrameChan ged(EventArgs e)
at System.Drawing.ImageAnimator.ImageInfo.set_Frame(I nt32 value)
at System.Drawing.ImageAnimator.AnimateImages50ms()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

note in some articals i read about calling the method begininvoke(delegate) so i did this and called the begininvoke method in the load procuder of my form and this what i have written

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim method As System.Delegate

Dim returnValue As IAsyncResult

returnValue = Me.BeginInvoke(method)


End Sub

i know there is something missing what is it i need a code exampe please


thanks in advance:)

.ben
12-11-2006, 06:11 PM
Your form is still loading so what you've written in your load event handler is invalid until the form has finished loading.

Why do you have that code in there?

MffM
12-12-2006, 11:18 AM
Tahnks for replying my message

that code i added to avoid the exception error message but it's not usefull and it's wrong.

now what i have to do to avoid the exeception message?
u said "Your form is still loading so what you've written in your load event handler is invalid until the form has finished loading.
"
how can i know when my form finish the loading to start my procuders?
note: usually this error appear when i close the form some time it's not

please advice

Thanks

.ben
12-12-2006, 09:44 PM
I took a closer look at your stack trace, and saw a class which I haven't used before: the imageanimator.

I played with it and it does run on another thread and I think in the eventhandler you've written, you are calling members of a UI control (a label I guess).

Since you are calling it from another thread you will get that exception. So you need to make that code thread safe.

Your current eventhandler could look like this:

Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
'stuff I'd like to do

x += 1
Label1.Text = "hihi" & x
End Sub


this is not thread safe!

to make it work you have to do it like this:

Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
'stuff I'd like to do

x += 1
ChangeTheLabel()
End Sub

Private Delegate Sub MyDelegate()

Private Sub ChangeTheLabel()
If Label1.InvokeRequired Then
Label1.BeginInvoke(New MyDelegate(AddressOf ChangeTheLabel))
Return
End If
Label1.Text = "hihi" & x
End Sub

Still I could be way off, if you could show some code I wouldn't have to do so much guesswork :)

MffM
12-13-2006, 11:06 AM
Hi

in my project i have many labels it contain animaited image
mainly all label have 3 methods
this is one of my label's method
Me.lblclose.Image = "lblclose.Image" ''' this image is animated
Private Sub lblclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblclose.Click
' when click on the close button we should confirm that all guops are set visable false the the main menu set visable true
gMainmenu.Visible = True
btnpell.Visible = True
btnCansel.Visible = True

gMainDishes.Visible = False
gRice.Visible = False
gPell.Visible = False
gapp.Visible = False
gdrink.Visible = False
gSweet.Visible = False
lblclose.Visible = False


End Sub


Private Sub lblclose_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblclose.MouseHover
lblclose.BorderStyle = BorderStyle.Fixed3D
End Sub

Private Sub lblclose_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblclose.MouseLeave
lblclose.BorderStyle = BorderStyle.None
End Sub

where i have to put the safe thread in my code and i have to do it to each label

thanks in advance

MffM
12-13-2006, 04:32 PM
i remove all event mouseover and mouse leave in the laels and the error sitll occured

i note something the error happen when i close the form
it enter the deispose method put it didn't finish it

this haapen some times not always

.ben
12-13-2006, 05:55 PM
I'm unable to duplicate that behaviour, even with labels containing animated gifs.

Put the threadsafe code in all code being called by the OnFrameChanged eventhandler and see if that helps.