1. Brief description
C# does not allow direct access to interface controls across threads, that is, controls created by one thread such as the main thread are not allowed to be directly accessed by other threads such as child threads. If you directly access or set properties, it will explode.The calling thread cannot access this object because another thread owns the object” and other similar errors.
There are usually two ways to set all control properties of other threads in one thread. One is to setCheckForIllegalCrossThreadCallsThe property value is false, which means that the call of the wrong thread is not captured. This method is only valid for the winforms interface framework. This property cannot be set in WPF, so you can only use the second method.
The second method is to implement control attribute settings through delegate, which needs to be done throughInvokeorBeginInvokeTo implement it, Invoke represents synchronization, BeginInvoke represents asynchronous, and the following lists code examples of cross-thread access controls in winforms and WPF interface frameworks.
2. Winforms mid-span thread access control
The general delegate method example code is as follows:
delegate void delegateSetText(string str);//Define a delegateprivate void SetText(string str) { if (InvokeRequired)//Discern whether it is called by other threads { delegateSetText delegatesetText01 = new delegateSetText(SetText); Invoke(delegatesetText01, new object [] {str}); return; } = str; }
Anonymous proxy can also be used, the example code is as follows:
delegate void delegateSetText(string str);//Define a delegateprivate void SetText(string str) { delegateSetText delegatesetText01 = delegate(string str01) { = str01; } Invoke(delegatesetText01, new object [] {str}); }
3. WPF mid-span thread access control
Cross-thread access controls are required in WPFDispatcher. Dispatcher is a thread dispatcher in WPF. It is used when the child thread refreshes the main thread (UI thread) (such as when binding or when the property is updated), and a dispatcher is created in the child thread to schedule the work items to the UI thread, so that the main thread schedules the refresh of the UI code. The sample code is as follows:
private void SetText(string str) { if (!())//Discern whether it is called by other threads { (() => SetText(str)); return; } = str; }
【Note】
Avoid "You cannot call Invoke or BeginInvoke on the control before creating a window handle." error.
A Form form must be fully created before it can call Invoke or BeginInvoke of itself or the controls on it.
After testing, when the execution of InitializeComponent() in the form constructor is completed, the form is not created; when the execution of the form constructor is completed, the form is not created;
After (), the form has been created.
In addition, you can judge if () before calling Invoke() to avoid abnormal interruption here.
at last
This is the end of this article about how to access controls in C# in cross-threads. For more related contents of C# cross-threads, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!