Before creating a window handle, you cannot call invoke or begininvoke on the control or call invoke or begininvoke on the control before creating a window handle.

Source: Internet
Author: User
You cannot call invoke or begininvoke on the control before creating a window handle.

Close a form today and report this error. "You cannot call invoke or begininvoke on the control before creating a window handle. ", You don't have to think about it. It must have been the place that was not released.
Now that you have encountered this problem, let alone the problem itself. Let's talk about other things.
The most common control of winform is the datagridview control. Many data operations are displayed by using the datagridview. Therefore, asynchronous calls are also quite a type of control.
For example:
1. Read a large amount of data from the database (the so-called paging reading is not in this category)
2. Operate the datagridview, and change or fill the dtagridview after a period of time.
3. The effect of the datagridview itself, such as the rotation delay wait or other
If asynchronous mode is not used, the system will definitely crash. When asynchronous mode is used, you may also need to pay attention to some situations.

A simple example:
There is a datagridview datagriin A showdialog form. I read data asynchronously, but I didn't finish reading the data. I closed the form. At this time, the data has been read and will be executed.
Datagridview. Source = ??
At this time, an error may occur. It may not be "You cannot call invoke or begininvoke on the control before creating a window handle. "This error is" resources have been released ". Let's look at the following methods.
1 This. Components attributes

 /// <Summary>///Required designer variable./// </Summary>PrivateSystem. componentmodel. icontainer components =Null;

Every designer. there is one such thing in CS. The icontainer interface is equivalent to a container, and all the things on a page are put in it. You drag a button or label to put it in it. I think, this is actually a container concept of WPF. You can find every control from the root, and icontainer can also find the control you want. This is used when active or inactive is activated.
Form release, components will also be released

  ///   <Summary>          /// Clean up any resources being used.  ///   </Summary>          ///   <Param name = "disposing">  True if managed resources shocould be disposed; otherwise, false.  </Param>          Protected   Override   Void Dispose ( Bool  Disposing ){  If (Disposing & (components! =Null  ) {Components. Dispose ();}  Base  . Dispose (disposing );} 

This method will be released, so it can be used as a basis for determining whether the form is released, but I do not recommend

2 This. isdisposed
This is to determine whether the component has been released. It is better to use this statement than components. The specific reason is that components may not be released after the form is closed, and this. the isdisposed form must have been released. When the form is in mid mode, the form may not be released due to thread or other reasons.

3 ishandlecreated
Whether to create a handle
When the sub-space handle is created and its parent handle is not created or released for other reasons, other problems may occur. ishandlecereated to check whether the parent handle exists or has been created

The above methods can be used in combination. The last two methods are used in my judgment.
Now let's talk about the two invoke and begininvoke
Control. Invoke: Run the delegate in the FIFO order on the thread that owns the control.
Control. begininvoke: Execute the delegate asynchronously on the thread that owns this control, that is, it may not be sequential execution. This is a bit familiar and seems to have been said.

Finally, let's talk about the solution:
In
Add 1 This. components = NULL 2 This. isdisposed 3 before invoke (...)
Ishandlecreated can return without executing invoke. Of course, it is only for the solution I have encountered. It may not be suitable for others, but it will never be separated from it.

 
 Set{If(Isdisposed |!This. Parent. ishandlecreated)Return;This. Invoke (system. Action)Delegate(){});}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.