当前位置:网站首页>Multithreaded learning 2- call control

Multithreaded learning 2- call control

2022-06-25 23:20:00 WhyLearnCode

1 Message mechanism to calling control

Windows GUI The program is based on the message mechanism , A main thread maintains a message pump . This message pumps windows The program never stops . If you operate from another thread windows Controls on forms , It will compete with the main thread , Cause unexpected results , Even deadlock. . therefore windows GUI There is a rule in programming , That is, the data of the control can only be operated by the thread that creates the control , Otherwise, it may produce unpredictable results .

because Windows Form controls are not thread safe in nature . Therefore, if there are two or more threads operating the state of a control appropriately (set value), May force the control into an inconsistent state . There may also be other thread related bug, Including contention and deadlock . So when you run an application in the debugger , If a thread other than the one that created the control attempts to call the control , The debugger raises a InvalidOperationException.

Common mistakes :

Invalid inter thread operation : Never create controls “xxxx” Thread access it
Invalid parameter

2 Three solutions

2.1 Turn off anomaly detection

Turning off exception detection is a direct way to avoid throwing exceptions , Is the simplest of the three methods . After testing, it is found that this method avoids exception throwing , But it can't guarantee the correctness of the program running results

Control.CheckForIllegalCrossThreadCalls = false;   

2.2 Through delegation security call

private TextBox _TextBox;
private string _Value;
delegate void SetTextCallback(TextBox TxtBox, String Value);
public Form5()
{
    
    InitializeComponent();
    //Control.CheckForIllegalCrossThreadCalls = false;
}

private void button1_Click(object sender, EventArgs e)
{
    
    Thread thread = new Thread(new ThreadStart(ThreadProcSafe));
    thread.Start();
}

private void ThreadProcSafe()
{
    
    for (int i = 0; i < 100; i++)
    {
    
        //this.SetText($"This text was set safely.{i}");
        SetText2(this.textBox1,$"textbox1+{i}");
        SetText2(this.textBox2, $"textbox2+{i}");
        Thread.Sleep(100);
    }
}

public void SetText(TextBox TxtBox, String Value)
{
    
    _TextBox = TxtBox;
    _Value = Value;
    _TextBox.Text = _Value;
    _TextBox.Refresh();
}
public void SetText2(TextBox TxtBox, String Value)
{
    
    _TextBox = TxtBox;
    _Value = Value;
    if (_TextBox.InvokeRequired)
    {
    
        SetTextCallback d = new SetTextCallback(SetText2);
        this.Invoke(d, new object[] {
     _TextBox, Value });
    }
    else
    {
    
        _TextBox.Text = Value;
    }
}
}

 Insert picture description here

 Insert picture description here

2.3 utilize BackgroundWorker Control

3 Reference resources

  1. c# Middle form use Invoke and BeginInvoke Detailed explanation
  2. How to call in multithreading winform Form control
原网站

版权声明
本文为[WhyLearnCode]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180605017517.html