惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
H
Hackread – Cybersecurity News, Data Breaches, AI and More
WordPress大学
WordPress大学
B
Blog RSS Feed
A
About on SuperTechFans
V
Visual Studio Blog
J
Java Code Geeks
U
Unit 42
人人都是产品经理
人人都是产品经理
Martin Fowler
Martin Fowler
Recent Announcements
Recent Announcements
M
MIT News - Artificial intelligence
IT之家
IT之家
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
C
Check Point Blog
博客园 - 叶小钗
P
Proofpoint News Feed
Microsoft Security Blog
Microsoft Security Blog
B
Blog
S
SegmentFault 最新的问题
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - hello csharp

[导入]Nhibernate引入自定义Membership和Role 推荐个不错的屏幕捕获程序 测试Google Docs 发布的文章.。。 Guidelines – a hidden feature for the Visual Studio Editor Gridview, ObjectDataSource Making life easy Applied MS Reporting Services 101 using Smart Client Database Provider-based ASP.NET Membership Provider HTMLEditor Provider - How to write a custom provider for ASP.NET 2.0 Firebug-javascript/css/ajax/dom调试器【这款不错】 Automatic Sql Server Backup Utility Using sqlserveragent LiveWriter测试 发布个小软件给大伙玩玩 [js小技巧]鼠标移到图片高亮度显示 dot net html分析类库 系统发邮件测试 Dumbster 【小技巧】如何得到一个网页的所有a标记 herf 链接代码 读新浪博客示例[源代码下载] Intellisense for the NHibernate XML Schemas Develop Reports Using Crystal Reports in .NET 2005
[小技巧]winfrom使用多线程
hello csharp · 2006-10-09 · via 博客园 - hello csharp

就拿我写的备份新浪博客的小程序说。。当我点备份按钮的时候,如果不用线程。。那窗体可能就会假死在那,一动不动的。给用户感觉很不爽的。。那要怎么解决呢??

你可能会觉的这个很简单。几行代码就搞定。

定义个方法
public void MyTest()
        {
            
for (int i = 0; i < 100; i++)
            {
                
this.textBox1.Text = i.ToString();
            }
        }

private void button1_Click(object sender, EventArgs e)
        {
            Thread myThread 
= new Thread(MyTest);
            myThread.Start();
        }

很遗憾。

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: 线程间操作无效: 从不是创建控件“textBox1”的线程访问它。

问题解决:

1.定义 委托
   delegate void myDelegate(int i);
   myDelegate mydelegate 
= null;

2.定义方法,显示消息

public void ShowMessage(int i)
        {
            
this.textBox1.Text = i.ToString();
            
this.progressBar1.Value = i;
        }

3.定义方法,驱动消息

public void MyEvent()
        {
            
for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(
100);
                
this.BeginInvoke(mydelegate, new object[] {i});
            
            }
        }

4: 运行
  private void button1_Click(object sender, EventArgs e)
        {
            mydelegate 
= new myDelegate(ShowMessage);
            Thread myThread 
= new Thread(MyEvent);//IsBackground 是否后台
            
//这个属性很重要 .如果 Thread IsBackground 等于false
            
// 当线程还没有结束时,你点了关闭按钮
            
// 将抛出An unhandled exception
            
//of type 'System.InvalidOperationException'
            
//occurred in System.Windows.Forms.dll 异常
            myThread.IsBackground = true;
            myThread.Start();
        }

全都代码:

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;namespace WinfromTheadTest
{
    
public partial class Form1 : Form
    {
        
delegate void myDelegate(int i);
        myDelegate mydelegate 
= null;public Form1()
        {
            InitializeComponent();
        }
private void button1_Click(object sender, EventArgs e)
        {
            mydelegate 
= new myDelegate(ShowMessage);
            Thread myThread 
= new Thread(MyEvent);//IsBackground 是否后台
            
//这个属性很重要 .如果 Thread IsBackground 等于false
            
// 当线程还没有结束时,你点了关闭按钮
            
// 将抛出An unhandled exception
            
//of type 'System.InvalidOperationException'
            
//occurred in System.Windows.Forms.dll 异常
            myThread.IsBackground = true;
            myThread.Start();
        }
public void ShowMessage(int i)
        {
            
this.textBox1.Text = i.ToString();
            
this.progressBar1.Value = i;
        }
public void MyEvent()
        {
            
for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(
100);
                
this.BeginInvoke(mydelegate, new object[] {i});
            
            }
        }
private void button2_Click(object sender, EventArgs e)
        {
          
//
        }

    }
}

点击源代码下载