How to Use BeginInvoke to solve the SetCurrentCellAddressCore exception and begininvoke

Source: Internet
Author: User

How to Use BeginInvoke to solve the SetCurrentCellAddressCore exception and begininvoke

The result is that after you double-click a cell to modify it, the data must be updated to the database in a timely manner, and the updated data must be re-bound to the control. However, an error will be reported when the data is re-bound, error message: The cause is that it causes reentrant calls to the SetCurrentCellAddressCore function. You can use BeginInvoke to solve this exception by searching on the Internet.

Public delegate void MyInvoke ();
Public void DoWork ()
{
MyInvoke mi = new MyInvoke (DataBind );
This. BeginInvoke (mi );
}

Call the DoWork () method when binding data! DataBind is the data binding method!



How is the Invoke and BeginInvoke implemented internally?

These two methods are mainly used to execute the given method on the thread created by the control.
Invoke uses SendMessage of Win32API,
UnsafeNativeMethods. PostMessage (new HandleRef (this, this. Handle), threadCallbackMessage, IntPtr. Zero, IntPtr. Zero );
BeginInvoke uses the PostMessage of Win32API
UnsafeNativeMethods. PostMessage (new HandleRef (this, this. Handle), threadCallbackMessage, IntPtr. Zero, IntPtr. Zero );
These two methods put a message into the message queue of the UI thread. When the UI thread processes the message, it will execute the passed method in its own context, in other words, all the threads that use BeginInvoke and Invoke call are executed in the main UI thread. Therefore, if these methods involve some static variables, do not consider locking.
In vs2003, the properties of controls created by calling the ui thread using subthreads are normal, but warnings are generated during compilation, but vs2005 and later versions have such problems, the following is the description on msdn.
"When you run code in the Visual Studio debugger, If you access a UI element from a thread and the thread is not the thread where the UI element is created, InvalidOperationException is thrown. The debugger raises this exception to warn you of dangerous programming operations.

C # The form_load event function of the program shows the cross-thread change control method controlbegininvoke ().

Controls cannot be directly modified in the thread.

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.