日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#为什么多线程控制winform需要用委托?

發布時間:2025/5/22 C# 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#为什么多线程控制winform需要用委托? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C#為什么多線程控制winform需要用委托?比如我新起了一個線程A,在A線程里要對winform的list控件里顯示數據,我需要用一個委托函數來實現。
因為winform是主線程創建的,你用另外一個線程來調用它就可能會出現兩個線程同時訪問同一個資源的問題,這個時候很容易出現錯誤,比如A線程改變窗口的顏色為紅色,B線程取窗口的顏色,如果這兩個線程正好碰到一起,可能A略先于B,那么就是B取出來的紅色,如果略后于那么就是原來的顏色,而這和CPU的繁忙度、時間片的輪轉是相關的,是一種隨機的情況,那么B取出來的顏色就不可靠了,因此為了避免這種狀況采用委托,B線程向A線程發出委托,由A線程來完成取色工作,那么可以保證取色工作的穩定性,結果也可靠

如果你實在想通過另一個線程訪問winform,可以在窗口的構造函數中加入
Control.CheckForIllegalCrossThreadCalls = false;
這樣可以屏蔽這個錯誤,不過建議還是用委托 這需要用到委托.
你先聲明一個委托,然后,把一個操作界面的函數托給他.
你的線程里只要調用這個委托就可以了. C#試寫一個多線程問題(委托,Invoke(),beginInvoke()) using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace _
{
????????public partial class Form1 : Form
????????{
????????????????public delegate void del(string str);
????????????????public delegate void del1(string str1);
????????????????Thread thread1,thread2;
????????????????public Form1()
????????????????{
????????????????????????InitializeComponent();
????????????????}

????????????????private void button1_Click(object sender, EventArgs e)
????????????????{
????????????????????????this.thread1 = new Thread(new ThreadStart(this.sa));
????????????????????????this.thread1.Start();
????????????????????????//MessageBox.Show(this,);
????????????????????????this.button1.Enabled = false;
????????????????????????//獲取當前的線程好
????????????????????????int thread1id = Thread.CurrentThread.GetHashCode();
????????????????????????string strThreadId = Convert.ToString(thread1id);
????????????????????????MessageBox.Show(this,strThreadId);
????????????????}????????????????

????????????????private void progressSet(string text)
????????????????{
????????????????????????????????progressBar1.Value = 0;
????????????????????????????????progressBar1.Maximum = 999999;
????????????????????????????????for (int i = 0; i < 999999; i++)
????????????????????????????????{
????????????????????????????????????????progressBar1.Value++;
????????????????????????????????}
????????????????????????????????progressBar1.Value = 0;????????????????????????
????????????????}

????????????????private void sa()
????????????????{
????????????????????????del pro = new del(progressSet);
????????????????????????this.Invoke(pro, new object[] { "hello" });
????????????????}

????????????????private void button2_Click(object sender, EventArgs e)
????????????????{
????????????????????????this.thread2 = new Thread(this.sa1);
????????????????????????thread2.Start();
????????????????????????this.button2.Enabled = false;
????????????????????????//獲取當前的線程好
????????????????????????int thread2id = Thread.CurrentThread.GetHashCode();
????????????????????????string strThreadId = Convert.ToString(thread2id);
????????????????????????MessageBox.Show(this, strThreadId);
????????????????}

????????????????private void sa1()
????????????????{
????????????????????????//其他操作????
????????????????????????//比如將界面的TextBox內容設置一下????
????????????????????????del1 mi1 = new del1(UpdateTextBox);
????????????????????????this.Invoke(mi1, new object[] { "我是一個文本框" });
????????????????}

????????????????//更新界面的方法????
????????????????private void UpdateTextBox(string str)
????????????????{
????????????????????????//更新????
????????????????????????this.textBox1.Text = str;
????????????????}
????????}
}

轉載于:https://blog.51cto.com/lovefly/274505

總結

以上是生活随笔為你收集整理的C#为什么多线程控制winform需要用委托?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。