当前位置: 首页 > news >正文

.NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)

1、错误的代码是:

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Windows.Forms;

namespace  WindowsFormsApplication5
{
    
public   partial   class  Form1 : Form
    {
        
public  Form1()
        {
            InitializeComponent();
        }

        
private   string  blog  =   " http://www.chenjiliang.com/ " ;


        
private   void  Form1_Load( object  sender, EventArgs e)
        {
            
new  System.Threading.Thread(ShowTime).Start();
        }

        
private   void  ShowTime()
        {
            textBox1.Text 
=  DateTime.Now.ToString();
        }
    }
}

会看到Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.的错误

2、增加一个Public static 类

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Windows.Forms;


namespace  WindowsFormsApplication5
{
    
public   static   class  ControlExtention
    {
        
public   delegate   void  InvokeHandler();

        
public   static   void  SafeInvoke( this  Control control, InvokeHandler handler)
        {
            
if  (control.InvokeRequired)
            {
                control.Invoke(handler);
            }
            
else
            {
                handler();
            }
        }
    }
}

3、build

4、这样写就好啦

 

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Windows.Forms;

namespace  WindowsFormsApplication5
{
    
public   partial   class  Form1 : Form
    {
        
public  Form1()
        {
            InitializeComponent();
        }
        
private   string  s  =   " http://www.chenjiliang.com/ " ;

        
private   void  Form1_Load( object  sender, EventArgs e)
        {
            
new  System.Threading.Thread(ShowTime).Start();
        }

        
private   void  ShowTime()
        {
            
this .SafeInvoke(()  =>
            {
                textBox1.Text 
=  DateTime.Now.ToString();
            });
        }
    }
}



 

转载于:https://www.cnblogs.com/arongbest/archive/2011/08/03/2126277.html

相关文章:

  • perl实现16进制数转换成10进制数
  • WFQ/CBWFQ/LLQ介绍
  • 优雅的jQuery插件xmlDOM
  • 知名摄影师、摄影器材专家赵嘉最新最全面的顶级摄影器材图书大系旋风上市!...
  • 字符,字节和编码 二
  • html5网站
  • freebsd手工配置网络
  • 在ubuntu下的比较好用的对比工具.meld. sudo apt-get install meld
  • HDU_3177 Crixalis's Equipment(贪心)
  • 初体验
  • HAProxy负载均衡器后续篇
  • 舆情监控服务模式
  • 使用drbd同步磁盘
  • 宏定义#define 完全用法
  • Oracle SQL*Loader 使用指南(第一部分)
  • php的引用
  • 【mysql】环境安装、服务启动、密码设置
  • 002-读书笔记-JavaScript高级程序设计 在HTML中使用JavaScript
  • AHK 中 = 和 == 等比较运算符的用法
  • Angular 4.x 动态创建组件
  • ES2017异步函数现已正式可用
  • java取消线程实例
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目
  • Python学习之路13-记分
  • React的组件模式
  • select2 取值 遍历 设置默认值
  • 搭建gitbook 和 访问权限认证
  • 提醒我喝水chrome插件开发指南
  • 智能情侣枕Pillow Talk,倾听彼此的心跳
  • $.ajax()
  • $.each()与$(selector).each()
  • (33)STM32——485实验笔记
  • (C语言)输入自定义个数的整数,打印出最大值和最小值
  • (独孤九剑)--文件系统
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)ssm高校运动会管理系统 毕业设计 020419
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (一)RocketMQ初步认识
  • (转)Linux下编译安装log4cxx
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • *(长期更新)软考网络工程师学习笔记——Section 22 无线局域网
  • .NET的数据绑定
  • /dev下添加设备节点的方法步骤(通过device_create)
  • @ 代码随想录算法训练营第8周(C语言)|Day53(动态规划)
  • @FeignClient 调用另一个服务的test环境,实际上却调用了另一个环境testone的接口,这其中牵扯到k8s容器外容器内的问题,注册到eureka上的是容器外的旧版本...
  • [ vulhub漏洞复现篇 ] Apache APISIX 默认密钥漏洞 CVE-2020-13945
  • [20180224]expdp query 写法问题.txt
  • [Android]一个简单使用Handler做Timer的例子
  • [Angularjs]asp.net mvc+angularjs+web api单页应用
  • [AX]AX2012 R2 出差申请和支出报告
  • [AX]AX2012开发新特性-禁止表或者表字段
  • [C puzzle book] types
  • [CUDA手搓]从零开始用C++ CUDA搭建一个卷积神经网络(LeNet),了解神经网络各个层背后算法原理