1 摇奖机
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace _01_摇奖机{ public partial class Form1 : Form { public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } private void button1_Click(object sender, EventArgs e) { while (true) { Random r = new Random(); label1.Text = r.Next(0, 10).ToString(); label2.Text = r.Next(0, 10).ToString(); label3.Text = r.Next(0, 10).ToString(); Thread.Sleep(1000); } } private void button2_Click(object sender, EventArgs e) { Thread th = new Thread(() => { Random r = new Random(); while (true) { label1.Text = r.Next(0, 10).ToString(); label2.Text = r.Next(0, 10).ToString(); label3.Text = r.Next(0, 10).ToString(); Thread.Sleep(1000); } }); th.IsBackground = true; th.Start(); } }}
2 拷贝文件
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 _02_文件拷贝{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //01 创建 线程 Thread th = new Thread(() => { //进行文件的读写操作 //01-01 读读读 using (FileStream fsReader = new FileStream("2015-04-14基础加强1.rar",FileMode.Open)) { //01-02 写写写 using (FileStream fsWrite = new FileStream( "a.rar",FileMode.Create)) { long count = fsReader.Length; long currentCount = 0; //设置每次读取的长度 byte[] bs = new byte[1024 * 1024]; int len; while ((len = fsReader.Read(bs,0,bs.Length))>0) { currentCount += len; progressBar1.Invoke( new Action ((xh) => { progressBar1.Value = xh; }), (int)(currentCount/count)*100 ); fsWrite.Write(bs,0,len); } } } }); //02 设置为后台线程 th.IsBackground = true; //03 启动 th.Start(); } }}
3 生产者消费者模式
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace stack_栈_生产者消费者{ class Program { static void Main(string[] args) { //02 定义栈 StackbzStack = new Stack ( ); //03 创建生产者线程 Thread thSC = new Thread(() => { //06 不断让生产者循环 while (true) { //03-01 通过for循环创建两个生产者 for (int i = 0; i < 2; i++) { //05 加锁 lock ("BZLock") { //03-02将生产的包子放入到栈中; bzStack.Push(new BaoZi()); //03-04 打印输出--生产者的编号 Console.WriteLine("生产者" + Thread.CurrentThread.ManagedThreadId); } //03-03 休息一下 Thread.Sleep(1000); } } }); thSC.IsBackground = true; thSC.Start(); //04 创建消费者 Thread thXF = new Thread(() => { //06 不断让生产者循环 while (true) { //04-01 通过 for 循环创建三个生产者 for (int i = 0; i < 3; i++) { //05 此时需要注意,当一个资源被多个线程使用的时候要注意加锁 lock ("BZLock") { //04-02 将生产者出栈--出栈之前先判断 if (bzStack.Count > 0) { BaoZi bz = bzStack.Pop(); //04-04 打印消费者 Console.WriteLine("消费者" + Thread.CurrentThread.ManagedThreadId); } } //04-03 休息一下 Thread.Sleep(1500); } } }); thXF.IsBackground = true; thXF.Start(); Console.ReadKey(); } #region 01 创建一个包子的类 public class BaoZi { } #endregion }}
4 异步调用
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Remoting.Messaging;using System.Text;using System.Threading.Tasks;namespace _04_异步调用委托{ class Program { static void Main(string[] args) { #region 01 (无参无返回值)委托 Action noNO = () => { Console.WriteLine("无参,无返回值"); }; noNO.BeginInvoke(null, ""); #endregion #region 02 (有参无返回值)委托 ActionyesNO= (s) => { Console.WriteLine(s); }; yesNO.BeginInvoke("有参,无返回值", null, ""); #endregion #region 03 (无参有返回值)委托 Func noYes = () => { return "OK"+"无参,有返回值"; }; noYes.BeginInvoke(Fun2, ""); #endregion #region 04 (有参有返回值)委托 Func yesYes = (s) => { return "ok;" + s; }; yesYes.BeginInvoke("有参,有返回值", Fun1, ""); #endregion Console.ReadKey(); } private static void Fun2(IAsyncResult ar) { //1类型转换 AsyncResult ar1 = ar as AsyncResult; //2 拿到委托 Func< string> f1 = ar1.AsyncDelegate as Func< string>; //3 通过委托调用endInvoke string s1 = f1.EndInvoke(ar1); //4 输入返回结果 Console.WriteLine(s1); } #region 通过回调函数,拿到异步委托的返回值 private static void Fun1(IAsyncResult ar) { //类型转换--将返回值IAsyncResult强制转换为AsyncResult AsyncResult ar1 = ar as AsyncResult; //拿到委托 Func f1 = ar1.AsyncDelegate as Func ; //只有委托才能调用endInvoke string s1 = f1.EndInvoke(ar1); Console.WriteLine(s1); } #endregion }}