C # multi-threaded operation: The operation between threads is invalid, and it is never accessed by the thread that did not create the control

The operation of the UI object must be performed in the thread that generates the UI object

The Form class has a bool type attribute InvokeRequired , which can be used to check whether the current thread is the thread that created the Form object (UI thread) -if true, it means that the current thread is not a UI thread, otherwise it is.

There are two methods need to pay attention to in TestForm, UIThread-used to simulate UI thread, WorkerThread-used to simulate user thread, members form1 and form2 are instantiated in UIThread, and their Show method is called, which changes in WorkerThread The Text property of form1.
When I run the code shown below, the following window will be displayed normally:
Insert picture description here
Program description:
(1) The main window TestForm is the startup form, and then two new instance forms form1 and form2
(2) Code form2.Show ( ); Display form 2
(3) When another thread WorkerThread method is executed in the UIThread thread (simulation UI thread), the current thread (WorkerThread) is not the thread that created the form2 (UIThread), the method is passed through the Invoke method Put it in the UI thread to execute. At this time, the value of InvokeRequired is true (indicating that the current thread is not a UI thread). Change the text attribute of form1 through the Invoke method, and finally display 3 windows

However, when commenting out form2.Show () ;:

Error: The inter-thread operation is invalid, and it is never accessed by the thread that created the control

Note:
Both form1 and form2 are created in UIThread, so the thread information they hold should be the same. So the values ​​of form1.InvokeRquired and form2.InvokeRquired are the same in any thread, that is, the value of InvokeRquire in WorkerThread should be true (because in different threads),
but if you comment out form2.Show () The show method of form2 is not used in the UIThread thread (simulated UI thread). When the simulated user thread is executed, form2.InvokeRquired is false so that it is directly operated in the user thread (change the attribute of form1 in the UI thread), The Invoke method is not used, so an error will be reported

Insert picture description here

TestForm form code

private Form form1;
private Form form2;      
public void UIThread()
{
    form1 = new Form();
    form2 = new Form();
    form2.Show();//注意这个地方!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    form2.Text = "form2";
    form1.Show();
    form1.Text = "form1";
    Thread thread = new Thread(new ThreadStart(WorkerThread));
    thread.Start();
}
//模拟用户线程,改变form1的text属性
public void WorkerThread()
{
    try
    {
        if (form2.InvokeRequired)
            form2.Invoke(new MethodInvoker(WorkerThread));
        else
            form1.Text = "This is from WorkerThread.";
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message,"错误提示");
    }
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    base.OnClosing(e);
    Application.Exit();

Program.cs code

 static void Main()
{
    TestForm tf = new TestForm();
    tf.Show();
    tf.UIThread();
    Application.Run();
}

Reference blog post:
https://blog.csdn.net/beelinkerlidejun/article/details/4772491

Published 18 original articles · praised 0 · visits 233

Guess you like

Origin blog.csdn.net/qq_39217004/article/details/105488747