现在的位置: 首页 > 综合 > 正文

control can’t call invoke or begininvoke before form was create

2014年01月03日 ⁄ 综合 ⁄ 共 1308字 ⁄ 字号 评论关闭

引用

Forms actually have a two phase initialization.  In the first phase (run when the constructor is called) creates the underlying object and its children and sets some initial properties.  At this point the actual window and control have not been created (thus
the handles don't exist yet).  After the form is created and Show orShowDialog is
called then the underlying window (and handle) are created.  When this occurs the OnLoad method of your form is called.  This is the first time that
you can assume the underlying window and its controls have handles.  You should call the base class implementation first.  Note also that your form still hasn't been displayed to the user yet.  The form is displayed after OnLoad returns. 
You should do any work that requires a window handle in OnLoad.  This is the first time that Invoke can
be called reliably.

Given the above description the error you are getting might make some sense now.  It is telling you that it can't find the windows handle.  It can't find the handle because it hasn't been created yet.  You called Invoke from
the constructor but the form won't be created untilOnLoad is called.  To resolve your problem you should move any of your logic that requiresInvoke to
the OnLoad method.  However you should go ahead and create any child controls in the constructor as normal.  You can also set any properties at this
time.  Invoke isn't required for any control you create in your constructor because by definition it'll be on the same UI thread as your form.

简单来说就是

对于异常情况,还需要找到更好的解决方案。

抱歉!评论已关闭.