Net prohibits cross-thread access to controls in principle, because this may cause errors. We recommend that you use a proxy to indirectly operate controls not created by the same thread.
The second method is to prohibit the compiler from checking cross-thread access, which can be used for access, but Control. checkforillegalcrossthreadcils = false is not guaranteed when no error occurs;
Recently I am working on a project and have encountered cross-thread access to page controls. however, an error is always prompted. You cannot modify the value of the control in the thread where the control is created in other threads. Later, the anonymous proxy is used, and the result is easily solved. the solution is as follows:
First, create a listbox, lable.
Form1_Load( Thread newthread = Thread( CrossThreadOperationControl CrossDelete = i = (i< listBox1.Items.Add( + i++ label1.Text = }
I hope this tips will be helpful for your learning and work. If you have a better solution to cross-thread access control problems, I would like to share it with you.
C # Cross-thread access control runtime error. You can solve this problem by using MethodInvoker:
Original code:
btnOK_Click( tslInfo.Text = Thread td = Thread( .tslInfo.Text = }
After modification:
btnOK_Click( tslInfo.Text = Thread td = Thread( .tslInfo.Text = MethodInvoker In = }
When we use winform applications, we usually encounter the problem of using the control information on the multi-threaded control interface. However, we cannot use traditional methods to solve this problem. I will introduce it in detail below.
First, let's look at the traditional method:
Form1_Load( Thread thread = thread.IsBackground = ( .textBox1.Text = Thread.Sleep( }
Run this code and we will see that the system throws an exception: Cross-thread operation not valid: Control 'textbox1' accessed from a thread other than the thread it was created on. this is because. net 2.0 and later enhanced the security mechanism, and does not allow direct cross-thread access to control attributes in winform. So how can we solve this problem? Several solutions are provided below.
In the first solution, we add the following code in the Form1_Load () method:
Form1_Load( Control.CheckForIllegalCrossThreadCalls = Thread thread = thread.IsBackground = }
After this code is added, the program can run normally. This code means that in this class, we do not check whether cross-thread calls are legal (if this sentence is not added, the operation is not abnormal, it indicates that the system and the default mode are not checked ). However, this method is not advisable. We can view the definition of the checkforillegalcrossthreadcils attribute and find that it is static. That is to say, no matter where the value is modified in the project, it will take effect globally. In addition, we usually check whether cross-thread access is abnormal. If someone else in the project modifies this attribute, our solution fails and we have to adopt another solution.
The second solution is to use delegate and invoke to control information from other threads. Many people write this control method on the Internet. However, I have read many such posts and it seems that there is no problem, but it has not actually solved this problem, first, let's look at the imperfect methods on the network:
FlushClient(); Form1_Load( Thread thread = thread.IsBackground= FlushClient fc = .BeginInvoke(fc); ( .textBox1.Text = Thread.Sleep( }
In this way, we can see that the exception of cross-thread access does not exist. However, a new problem occurs and the interface does not respond. Why does this problem occur? We just need to refresh the new thread infinitely and theoretically it will not affect the main thread. Otherwise, this method is equivalent to "injecting" The New thread into the main control thread, and it achieves the control of the main thread. As long as this thread does not return, the main thread will never respond. Even if the new thread does not use an infinite loop, it can return. The use of multithreading in this way also loses its original meaning.
Now let's look at the recommended solution (this solution is recommended ):
FlushClient(); Form1_Load( Thread thread = thread.IsBackground = ( Thread.Sleep( (.textBox1.InvokeRequired) FlushClient fc = .Invoke(fc); .textBox1.Text = }
After running the above code, we can see that the problem has been solved. By waiting for Asynchronization, we will not always hold the control of the main thread, in this way, you can control winform multi-threaded controls without cross-thread call exceptions.
For the questions raised by Shen Shan laolin, I recently found a better solution and used the asynchronous call of delegate. Let's take a look:
FlushClient(); Form1_Load( Thread thread = thread.IsBackground = FlushClient= FlushClient.BeginInvoke(, ( .textBox1.Text = Thread.Sleep( }
This method can also be directly simplified to (because the asynchronous mode of delegate is an asynchronous thread ):
FlushClient(); Form1_Load( Thread thread = FlushClient= FlushClient.BeginInvoke(, ( .textBox1.Text = Thread.Sleep( }