首页 > 编程语言 >C#之跨线程访问控件属性

C#之跨线程访问控件属性

时间:2022-11-05 10:56:01浏览次数:41  
标签:控件 string C# Text usl DoAction 线程 str btn

在窗体设计中,会经常遇到跨线程访问窗体控件,如果直接访问会报错,那怎么办呢?直接上代码

代码为一个类,实际运用的时候直接实例化调用即可

 

 

  1 class CrossThreadUpdateUIClass
  2     {
  3        
  4             public void ChangeBtnTxt(Button Btn, string Str)
  5             {
  6                 Action<Button, string> DoAction = delegate (Button btn, string str)
  7                 {
  8 
  9                     if (btn.InvokeRequired)
 10                     {
 11                         btn.Text = str;
 12                         //btn.BackColor = ; ; ;
 13                     }
 14                     else
 15                     {
 16                         btn.Text = str;
 17                     }
 18                 };
 19             Btn.Invoke(DoAction, Btn, Str);
 20 
 21 
 22         }
 23             public void ChangeLabelTxt(Label Lb, string Str)
 24             {
 25 
 26                 Action<Label, string> DoAction = delegate (Label lb, string str)
 27                 {
 28 
 29 
 30                     if (lb.InvokeRequired)
 31                     {
 32                         lb.Text = str;
 33                         //btn.BackColor = ; ; ;
 34                     }
 35                     else
 36                     {
 37                         lb.Text = str;
 38 
 39                     }
 40 
 41                 };
 42 
 43                 Lb.Invoke(DoAction, Lb, Str);
 44 
 45             }
 46             public void ChangeControllTxt(Control Ctl, string Str)
 47             {
 48                 Action<Control, string> DoAction = delegate (Control ctl, string str)
 49                 {
 50 
 51 
 52                     if (ctl.InvokeRequired)
 53                     {
 54                         ctl.Text = str;
 55                         //btn.BackColor = ; ; ;
 56                     }
 57                     else
 58                     {
 59                         ctl.Text = str;
 60 
 61                     }
 62 
 63                 };
 64 
 65                 Ctl.Invoke(DoAction, Ctl, Str);
 66 
 67             }
 68             public void ChangeUserLabelValue(UserLabel userlabel, string text)
 69             {
 70                 Action<UserLabel, string> DoAction = delegate (UserLabel usl, string str)
 71                 {
 72                 
 73 
 74                     if (usl.InvokeRequired)
 75                     {
 76                         usl.Text = str;
 77                         if(str=="PASS")
 78                         {
 79                             usl.BackColor = Color.Green;
 80                         }
 81                        else if(str == "FAIL")
 82                         {
 83                             usl.BackColor = Color.Red;
 84                         }
 85                     }
 86                     else
 87                     {
 88                         usl.Text = str;
 89                         if (str == "PASS")
 90                         {
 91                             usl.BackColor = Color.Green;
 92                         }
 93                         else if (str == "FAIL")
 94                         {
 95                             usl.BackColor = Color.Red;
 96                         }
 97                     }
 98 
 99                 };
100 
101             userlabel.Invoke(DoAction, userlabel, text);
102 
103             }
104             
105             public void UpdateControlAsync(Action act)
106             {
107                 Task task = new Task(act);
108 
109                 task.Start();
110             }
111       }

 

标签:控件,string,C#,Text,usl,DoAction,线程,str,btn
From: https://www.cnblogs.com/HomeSapiens/p/16859780.html

相关文章

  • 全球名校AI课程库(38)| 马萨诸塞大学 · 自然语言处理进阶课程『Advanced Natural Langu
    ......
  • 长事务 (Long Transactions)
    长事务长事务用于支持AutoCAD参照编辑功能,对于ObjectARX应用程序非常有用。这些类和函数为应用程序提供了一种方案,用于签出实体以进行编辑并将其签回其原始位......
  • Python 爬虫之多线程
    网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁、......
  • ClickHouse 数据类型
    1、整形固定长度的整型,包括有符号整型或无符号整型。整型范围(-2n-1~2n-1-1):Int8-[-128:127]Int16-[-32768:32767]Int32-[-2147483648:2147483647]Int64-......
  • .net core web html中午查看源码显示乱码
    在.NetCoreWeb的视图获取从后台传来的数据的时候,默认使用的是UnicodeRanges.BasicLatin进行的编码,所以在前台显示中文,而在查看源码的时候,就会发现中文进行过编码操作......
  • #yyds干货盘点#【愚公系列】2022年11月 微信小程序-(rich-text)富文本和(text)文本的
    前言富文本格式(RichTextFormat)即RTF格式,又称多文本格式,是由微软公司开发的跨平台文档格式。大多数的文字处理软件都能读取和保存RTF文档。它是一种方便于不同的设备、系......
  • Codeforces Round #832 (Div. 2) A-D
    比赛链接A题解知识点:贪心。我们考虑把正数和负数分开放,显然把负数和正数放在一起的结果不会更优。时间复杂度\(O(n)\)空间复杂度\(O(1)\)代码#include<bits/std......
  • leetcode 2437
    简介简单题codeclassSolution{publicIntegercount=0;publicvoiddfs(StringBuffertime,int[]aws,intrlt,intmaxNumber){if(rlt>=m......
  • Capture One Pro 22 (RAW图像处理软件)中文直装版mac/win
    CaptureOnePro22forMac是一款专业的RAW文件转换器和图像编辑软件,拥有更新的处理引擎、市场领先的性能和强大的新功能,可为500多台高端相机提供具有美丽色彩和令人难......
  • 模板匹配(createTrackbar函数这样用)
    一、模板匹配模板匹配(TemplateMatching)就是在一幅图像中寻找和模板图像(template)最相似的区域,该方法原理简单计算速度快,能够应用于目标识别,目标跟踪等多个领域。二、原理......