using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 线程和异步任务
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 线程休眠
/// <summary>
/// 线程休眠
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
// Thread 线程 Sleep 休眠
// sleep 线程休眠3s
Thread.Sleep(3000);
MessageBox.Show("主线程休眠");
}
#endregion
#region 是否后台线程
/// <summary>
/// 是否是后台线程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
Thread th;// 全局的线程
private void button2_Click(object sender, EventArgs e)
{
// ThreadStart 定义线程的委托方法
ThreadStart ts = new ThreadStart(Test1);
// 创建线程
th = new Thread(ts);
// 是否是后台线程
// 后台线程随着主线程退出而退出
th.IsBackground = true;
// 开启线程
th.Start();
}
public void Test1()
{
try
{
// 组织分线程的执行 不会阻止主线程的执行
Thread.Sleep(10000);
MessageBox.Show("分线程执行");
}
catch (Exception)
{
throw;
}
}
#endregion
/// <summary>
/// 窗体正在关闭的事件 停止分线程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (th!=null)
{
th.Abort();// 中断分线程
}
}
#region 带参数的线程的方法
/// <summary>
/// 带参数的线程点击按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
Thread th = new Thread(Test2);
th.Start(2);// 开启分线程 传递参数
new Thread(() =>
{
// 分线程
}).Start();
}
public void Test2(object a)
{
MessageBox.Show("检测出来的半点个数为:" + Convert.ToInt16(a));
}
#endregion
#region 线程池
private void button4_Click(object sender, EventArgs e)
{
// ThreadPool 线程池 将方法排入队列以便执行 方法在线程池里面有可用的线程的时候执行
// QueueUserWorkItem 用户工作队列
// 参数一:线程方法
// 参数二:传递参数
ThreadPool.QueueUserWorkItem(obj =>
{
Thread.Sleep(10000);
MessageBox.Show(obj.ToString());
},2);
// ThreadPool.SetMaxThreads;// 最大活跃的线程个数
ThreadPool.QueueUserWorkItem(Test3, 2);
}
public void Test3(object a)
{
// 分线程不能更新UI
// 特殊的属性可以直接更改
// this.button4.BackColor = Color.Black;
// this 窗体对象
// Inoke 调用 回到窗体创建的线程更新UI
// 同步执行委托方法
//this.Invoke(new Action(() =>
//{
// this.button4.Text = "更新UI";
//}));
// 异步执行委托方法
this.BeginInvoke(new MethodInvoker(() =>
{
this.button4.Text = "更新UI";
}));
}
#endregion
#region Task异步任务实现分线程
private void button5_Click(object sender, EventArgs e)
{
#region 创建任务的三种方式
#region 1.使用New Task 创建
Task t1 = new Task(new Action(() =>
{
Console.WriteLine("ZW1");
}));
t1.Start();// 开启任务
#endregion
#region 2.Task使用静态方法 Run
Task.Run(new Action(() =>
{
Console.WriteLine("ZW2");
}));
#endregion
#region 3.使用任务工厂创建
Task.Factory.StartNew(() =>
{
Console.WriteLine("ZW3");
});
#endregion
#endregion
}
#endregion
#region Async和Await
private async void button6_Click(object sender, EventArgs e)
{
// 使用async和await解决多层嵌套回调
await Task.Run(Test5);
await Task.Run(Test6);
}
public void Test5()
{
Console.WriteLine("ZW");
}
public void Test6()
{
Console.WriteLine("zw");
}
#endregion
#region Thread 应用异常捕获场景
private void button7_Click(object sender, EventArgs e)
{
Thread th = new Thread(Test7);
th.Start(0);
}
public void Test7(object a)
{
try
{
int num1 = (int)a;
int num2 = 10 / num1;
Console.WriteLine(num2);
}
catch (Exception ex)
{
// Exception 所有异常的基类
// IndexOutOfRangeException 索引值越界的异常
// IOException 文件读写的异常
// DivideByZeroException 除数为零的异常
// DirectoryNotFoundException 文件夹不存在异常
Console.WriteLine(ex.Message);// 打印异常信息
}
finally
{
}
}
#endregion
#region Task异常捕获
private void button8_Click(object sender, EventArgs e)
{
Task t1 = new Task(Test8);
t1.Start();
try
{
t1.Wait();// 这种只能在Release环境下捕获异常
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public void Test8()
{
int a = 10;
int b = 0;
int c = a / b;
}
#endregion
}
}
线程和异步任务
最新推荐文章于 2025-05-10 00:04:20 发布
169万+

被折叠的 条评论
为什么被折叠?



