`

.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();
            });
        }
    }
}

 

分享到:
评论

相关推荐

    ASP.NET3.5.pdf

    全书内容包括了解ASP.NET、认识C# 3.5、认识面向对象、ASP.NET基础控件、ASP.NET高级控件、数据库与ADO.NET、数据库基础、ASP.NET操作数据库、ASP.NET访问其他数据源、ASP.NET MVC、LINQ及Lambda表达式、WCF应用开发...

    ASP.NET 3.5开发大全 (中文 PDF 完整书签 非扫描)

    第1章:第一章详细的介绍了ASP.NET基础以及.NET平台的历史以及前瞻,在第一章中,读者能够学会如何安装Visual Studio 2008以及SQL Server 2005进行应用程序开发基础,第一章还讲解了开发环境的使用技巧以方便读者...

    ASP.NET 3.5 开发大全

    《ASP.NET 3.5 开发大全》内容包括了解ASP.NET、认识C# 3.5、认识面向对象、ASP.NET基础控件、ASP.NET高级控件、数据库与ADO.NET、数据库基础、ASP.NET操作数据库、ASP.NET访问其他数据源、ASP.NET MVC、LINQ及...

    ASP.NET3.5从入门到精通

    1.1.5 ASP.NET 3.5 新增控件 1.1.6 ASP.NET 3.5 AJAX 1.2 .NET 应用程序需框架 1.2.1 什么是.NET 应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装...

    ASP.NET 3.5 开发大全11-15

    1.1.5 ASP.NET 3.5新增控件 1.1.6 ASP.NET 3.5 AJAX 1.2 .NET应用程序需框架 1.2.1 什么是.NET应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装Visual ...

    ASP.NET 3.5 开发大全1-5

    1.1.5 ASP.NET 3.5新增控件 1.1.6 ASP.NET 3.5 AJAX 1.2 .NET应用程序需框架 1.2.1 什么是.NET应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装Visual ...

    ASP.NET 3.5 开发大全word课件

    1.1.5 ASP.NET 3.5新增控件 1.1.6 ASP.NET 3.5 AJAX 1.2 .NET应用程序需框架 1.2.1 什么是.NET应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装Visual ...

    ASP.NET 3.5 开发大全 压缩包2

    第17章:介绍了ASP.NET MVC框架的基本知识,ASP.NET MVC框架是下一代ASP.NET应用程序框架,了解ASP.NET MVC基本知识能够为下一代ASP.NET应用程序开发做好准备。 第18、19章:介绍了WCF和WPF应用程序的开发,WCF和WPF...

    ASP.NET 3.5开发大全

    全书内容包括了解ASP.NET、认识C# 3.5、认识面向对象、ASP.NET基础控件、ASP.NET高级控件、数据库与ADO.NET、数据库基础、ASP.NET操作数据库、ASP.NET访问其他数据源、ASP.NET MVC、LINQ及Lambda表达式、WCF应用开发...

    ASP.NET 3.5 开发大全 压缩包1

    第17章:介绍了ASP.NET MVC框架的基本知识,ASP.NET MVC框架是下一代ASP.NET应用程序框架,了解ASP.NET MVC基本知识能够为下一代ASP.NET应用程序开发做好准备。 第18、19章:介绍了WCF和WPF应用程序的开发,WCF和WPF...

    ASP.NET 3.5 开发大全(下)

    全书内容包括了解ASP.NET、认识C# 3.5、认识面向对象、ASP.NET基础控件、ASP.NET高级控件、数据库与ADO.NET、数据库基础、ASP.NET操作数据库、ASP.NET访问其他数据源、ASP.NET MVC、LINQ及Lambda表达式、WCF应用开发...

    .NET 3.5新功能介绍

    .NET 3.5的新特性包括: 自动属性(Auto-Implemented Properties) 隐含类型局部变量(Local Variable Type Inference) 匿名类型(Anonymous Types) 对象与集合初始化器(Object and Collection Initializers) 扩展...

    ASP.NET 3.5 开发大全.part1

    全书内容包括了解ASP.NET、认识C# 3.5、认识面向对象、ASP.NET基础控件、ASP.NET高级控件、数据库与ADO.NET、数据库基础、ASP.NET操作数据库、ASP.NET访问其他数据源、ASP.NET MVC、LINQ及Lambda表达式、WCF应用开发...

    ASP.NET 3.5 开发大全 (上)

    第17章:介绍了ASP.NET MVC框架的基本知识,ASP.NET MVC框架是下一代ASP.NET应用程序框架,了解ASP.NET MVC基本知识能够为下一代ASP.NET应用程序开发做好准备。 第18、19章:介绍了WCF和WPF应用程序的开发,WCF和WPF...

Global site tag (gtag.js) - Google Analytics