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.