溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#中如何解決多線程更新界面的錯誤問題

發(fā)布時間:2021-10-11 16:32:37 來源:億速云 閱讀:134 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)C#中如何解決多線程更新界面的錯誤問題,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

    由于一個線程的程序,如果調(diào)用一個功能是阻塞的,那么就會影響到界面的更新,導致使用人員操作不便。所以往往會引入雙線程的工作的方式,主線程負責更新界面和調(diào)度,而次線程負責做一些阻塞的工作。

    這樣做了之后,又會導致一個常見的問題,就是很多開發(fā)人員會在次線程里去更新界面的內(nèi)容。比如下面的例子:

    C#中如何解決多線程更新界面的錯誤問題

    在上面的例子里,創(chuàng)建Win forms應(yīng)用,然后增加下面的代碼:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var thread2 = new System.Threading.Thread(WriteTextUnsafe);
                thread2.Start();
            }
    
            private void WriteTextUnsafe() =>
                textBox1.Text = "This text was set unsafely.";
        }
    }

    這里就是使用線程來直接更新界面的內(nèi)容,就會導致下面的出錯:

    C#中如何解決多線程更新界面的錯誤問題

    這樣在調(diào)試的界面就會彈出異常,但是有一些開發(fā)人員不是去解決這個問題,而是去關(guān)閉開發(fā)工具的選項,不讓彈出這個界面?;蛘卟皇褂谜{(diào)試方式。

    其實上面的代碼是有問題的,我們需要把它們修改為下面這種形式:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var threadParameters = new System.Threading.ThreadStart(
                    delegate { WriteTextSafe("This text was set safely."); });
                var thread2 = new System.Threading.Thread(threadParameters);
                thread2.Start();
            }
    
            public void WriteTextSafe(string text)
            {
                if (textBox1.InvokeRequired)
                {
                    // Call this same method but append THREAD2 to the text
                    Action safeWrite = delegate { WriteTextSafe($"{text} (THREAD2)"); };
                    textBox1.Invoke(safeWrite);
                }
                else
                    textBox1.Text = text;
            }
        }
    }

    這樣問題,就得了解決。這里使用了委托的方式。

    關(guān)于“C#中如何解決多線程更新界面的錯誤問題”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

    向AI問一下細節(jié)

    免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI