C#_多线程使用初识
来源:互联网 发布:淘宝卖家设置发货时间 编辑:程序博客网 时间:2024/06/04 00:54
学习C#中多线程方法(一):
WinFrom应用中,创建一个单独的线程执行执行一定的操作,为了向用户现实程序进度,在form中添加progressBar 控件现实进度,通过label显示提示字串。
1. 创建Form,添加progressBar,label, Button 控件
2. 添加引用 system.threading. 这个引用用来创建线程启动操作。
最初的程序为:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Threading;namespace ProgressBarTest{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(RunOnTimerThread)); thread.IsBackground = true; thread.Start(); } private void RunOnTimerThread() { if (progressBar1.Value != 100) { progressBar1.Value += 1; //抛出异常 } if (progressBar2.Value != 100) progressBar2.Value += 10; //抛出异常 Thread.Sleep(1000); label1.Text = System.DateTime.Now.ToString(); //抛出异常 } }}
在编译过程中会返回以下exception: Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on.这里涉及到多线程控制的安全问题,在windows的编程中有这样一个原则,在窗体创建线程之外的任何线程中都不能操作窗体。要解决这个问题,可以调用控件提供的Invoke和BeginInvoke把调用“封送回”窗体创建线程,让程序在窗体创建线程上修改控件属性。下面是修改后的程序:
private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(StartSomeWorkFromUIThread)); thread.IsBackground = true; thread.Start(); } private void StartSomeWorkFromUIThread() { if (this.InvokeRequired) BeginInvoke(new EventHandler(RunOnTimerThread), null); else RunOnTimerThread(this, null); } private void RunOnTimerThread(object sender, EventArgs e) { if (progressBar1.Value != 100) { progressBar1.Value += 1; } if (progressBar2.Value != 100) progressBar2.Value += 10; Thread.Sleep(1000); label1.Text = System.DateTime.Now.ToString(); }
参考:http://www.cnblogs.com/jianglai11/articles/1708330.htmlhttp://www.cnblogs.com/net66/archive/2005/08/03/206132.html
- C#_多线程使用初识
- C#_多线程创建和使用
- C#_多线程_ 整理
- C# 多线程的使用(Thread)-初识多线程
- C语言_数组初识总结
- Linux C 多线程【1】--简单初识
- C/C++_多和多线程_demo1
- C#使用_多线程处理
- C/C++_多核多线程编程_互斥锁
- 初识多线程
- 初识多线程
- 初识多线程
- 初识多线程
- 初识多线程
- 初识多线程
- 初识多线程
- [C++]_[初级]_[pthread多线程之生产者消费者模型]
- C#.net使用多线程
- SQL Server 和Oracle 数据类型对应
- 记录一下八款开源 Android 游戏引擎
- 怎么搭建 Octopress 在 Github 仅供学习
- matlab的基本用法
- ASP.NET AJAX中UpdatePanel的工作原理
- C#_多线程使用初识
- CursorAdapter
- Tomcat的class加载的优先顺序一览
- X264和JM的区别
- MIDI制作的小知识和小经验
- 图 邻接表
- pig
- DataStage 简介
- 编译(1)