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

引用

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.

简单来说就是



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值