zoukankan
html css js c++ java
c# 多线程基本操作
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
winfromDeom
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
button2.Enabled
=
false
;
}
//
声名一个线程
public
Thread t;
public
delegate
void
upUI();
private
void
button1_Click(
object
sender, EventArgs e)
{
progressBar1.Value
=
0
;
button1.Enabled
=
false
;
button2.Enabled
=
true
;
//
实列化一个线程
t
=
new
Thread(
new
ThreadStart(doThread));
//
设置是否是后台执行的线程
t.IsBackground
=
true
;
//
开始执行这个线程
t.Start();
}
private
void
doThread()
{
//
更新UI委托
upUI upui
=
new
upUI(upDataUi);
try
{
while
(
true
)
{
Thread.Sleep(
0
);
//
开始更新UI
this
.Invoke(upui);
}
}
finally
{
Thread.Sleep(
5000
);
}
}
private
void
button2_Click(
object
sender, EventArgs e)
{
//
设置为终止状态
t.Abort();
//
阻塞这个线程
t.Join();
button1.Enabled
=
true
;
button2.Enabled
=
false
;
}
/**/
///
<summary>
///
执行UI更新的方法
///
</summary>
private
void
upDataUi()
{
if
(progressBar1.Value
!=
100000
)
{
progressBar1.Value
=
progressBar1.Value
+
10
;
label1.Text
=
progressBar1.Value.ToString();
}
else
{
t.Abort();
t.Join();
button1.Enabled
=
true
;
button2.Enabled
=
false
;
}
}
private
void
Form1_FormClosing(
object
sender, FormClosingEventArgs e)
{
t.Abort();
t.Join();
}
}
}
查看全文
相关阅读:
click事件——背景高亮
color——RGB转16进制
导航栏高亮设置
layui table 渲染完成后,怎样拿到表个里的所有数据
解决 AttributeError: 'dict' object has no attribute 'has_key' 错误的方法
2019年总结:醒悟还为时不晚
Worker Services读取配置后,发布Windows出现的问题及解决
C# 读取配置(详细操作,让我们一起共同成长)
程序不包含适合于入口点的静态“Main”方法
.NET Core3.0-Worker Services
原文地址:https://www.cnblogs.com/wubiyu/p/818810.html
最新文章
Java复习之基础程序设计知识点
Online 学习网站
C++复习笔记
数据库复习之数据库系统结构
ALAssetsLibrary获取相册图片
JavaWeb——Web服务器
JavaWeb——基本原理
MySQL——JDBC
MySQL——规范数据库设计
MySQL——权限管理和备份
热门文章
MySQL——索引
MySQL——事物
MySQL——数据库级别的MD5加密
MySQL——函数
MySQL——子查询、分组过滤
python——多进程共享资源
python——多进程学习
python——多线程学习
Windows下安装 es-head 插件与使用
js把字符串(yyyymmdd)转换成日期格式(yyyy-mm-dd)
Copyright © 2011-2022 走看看