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 demo_1018
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(run));
thread.Start();
}
public void run()
{
int sum = 0;
for (int i = 0; i < 1000; i++)
{
sum = sum + i;
}
Console.WriteLine(sum);
}
private void button2_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ParameterizedThreadStart(run1));
thread.Start("你好");
}
public void run1(object A)
{
string str = A as string;
Console.WriteLine(str);
}
public delegate int Sum(int a, int b);
private void button3_Click(object sender, EventArgs e)
{
Sum sum = new Sum(test);
//int num = test(3, 5);
//Console.WriteLine(num);
int num = sum.Invoke(3, 5);
Console.WriteLine(num);
}
public int test(int a, int b)
{
return a + b;
}
private void button4_Click(object sender, EventArgs e)
{
Thread thread = new Thread(test2);
thread.Start();
}
public void test2()
{
textBox1.Invoke(new Action(() => { textBox1.Text = "123"; }));
}
public delegate void Operation();
Operation operation;
private void button5_Click(object sender, EventArgs e)
{
operation = test3;
Thread thread = new Thread(run2);
thread.Start();
}
public void run2()
{
// operation.Invoke();
this.Invoke(operation);
}
public void test3()
{
textBox1.Text = "123";
}
//使用lamada表达式
private void button6_Click(object sender, EventArgs e)
{
Thread thread = new Thread(() => { Console.WriteLine("HELLO WORLD"); });
thread.Start();
}
private void button7_Click(object sender, EventArgs e)
{
Thread thread = new Thread(delegate () { Console.WriteLine("HELLO WORLD"); });
thread.Start();
}
private void button8_Click(object sender, EventArgs e)
{
Thread thread = new Thread(() =>
{
Thread.Sleep(6000);
Console.WriteLine("start a new thread");
});
thread.IsBackground = true;
thread.Start();
Console.WriteLine("main thread end");
}
private void button9_Click(object sender, EventArgs e)
{
BookShop bookShop = new BookShop();
Thread thread = new Thread(bookShop.Sale);
Thread thread1 = new Thread(bookShop.Sale);
thread.Start();
thread1.Start();
}
private delegate void setTextValueCallBack(int value);
//声明回调
private setTextValueCallBack setCallBack;
private void button10_Click(object sender, EventArgs e)
{
setCallBack = SetValue;
Thread thread = new Thread(run4);
thread.IsBackground = true;
thread.Start();
}
public void run4()
{
for (int i = 0; i < 10000; i++)
{
this.Invoke(setCallBack, i);
}
}
public void SetValue(int value)
{
textBox1.Text = value.ToString();
}
//异步方法
private void button11_Click(object sender, EventArgs e)
{
//是确认线程的唯一标识符
Console.WriteLine($"***************btnAsync_Click Start {Thread.CurrentThread.ManagedThreadId}");
//Action<string> action = this.DoSomethingLong;
调用委托(同步调用)
//action.Invoke("btnAsync_Click_1");
异步调用委托
//action.BeginInvoke("btnAsync_Click_2", null, null);
Action<string> action = this.DoSomethingLong;
for (int i = 0; i < 5; i++)
{
//Thread.Sleep(5);
string name = string.Format($"btnAsync_Click_{i}");
action.BeginInvoke(name, null, null);
}
Console.WriteLine($"***************btnAsync_Click End {Thread.CurrentThread.ManagedThreadId}");
}
//同步
private void button12_Click(object sender, EventArgs e)
{
Console.WriteLine($"****************btnSync_Click Start {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
for (int i = 0; i < 5; i++)
{
string name = string.Format($"btnSync_Click_{i}");
this.DoSomethingLong(name);
}
}
private void DoSomethingLong(string name)
{
Console.WriteLine($"****************DoSomethingLong {name} Start {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
long lResult = 0;
for (int i = 0; i < 1000000000; i++)
{
lResult += i;
}
Console.WriteLine($"****************DoSomethingLong {name} End {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} {lResult}***************");
}
// 需求:异步多线程执行完之后再打印出下面这句,下面代码没有实现该功能
private void button13_Click(object sender, EventArgs e)
{
Console.WriteLine($"****************btnAsyncAdvanced_Click Start {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
Action<string> action = this.DoSomethingLong;
action.BeginInvoke("btnAsyncAdvanced_Click", null, null);
// 需求:异步多线程执行完之后再打印出下面这句
Console.WriteLine($"到这里计算已经完成了。{Thread.CurrentThread.ManagedThreadId.ToString("00")}。");
Console.WriteLine($"****************btnAsyncAdvanced_Click End {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
}
private void button14_Click(object sender, EventArgs e)
{
Console.WriteLine($"****************btnAsyncAdvanced_Click Start {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
Action<string> action = this.DoSomethingLong;
AsyncCallback asyncCallback = p => { Console.WriteLine($"到这里计算已经完成了。{Thread.CurrentThread.ManagedThreadId.ToString("00")}。"); };
action.BeginInvoke("btnAsyncAdvanced_Click", asyncCallback, null);
Console.WriteLine($"****************btnAsyncAdvanced_Click End {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
}
private void button15_Click(object sender, EventArgs e)
{
// 需求:异步多线程执行完之后再打印出下面这句
Console.WriteLine($"****************btnAsyncAdvanced_Click Start {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
Action<string> action = this.DoSomethingLong;
IAsyncResult asyncResult = null;
// 定义一个回调
AsyncCallback callback = p =>
{
// 比较两个变量是否是同一个
Console.WriteLine(object.ReferenceEquals(p, asyncResult));
Console.WriteLine($"到这里计算已经完成了。{Thread.CurrentThread.ManagedThreadId.ToString("00")}。");
};
// 回调作为参数
asyncResult = action.BeginInvoke("btnAsyncAdvanced_Click", callback, null);
Console.WriteLine($"****************btnAsyncAdvanced_Click End {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
}
//上述结论: BeginInvoke的返回值就是callback委托的参数。
private void button16_Click(object sender, EventArgs e)
{
Func<int> action = () =>
{
Thread.Sleep(2000);
return DateTime.Now.Day;
};
//输出委托同步调用的返回值
int num = action.Invoke();
Console.WriteLine(num);
//委托的异步调用
IAsyncResult asyncResult = null;
AsyncCallback asyncCallback = p => { Console.WriteLine(p.AsyncState); };
asyncResult = action.BeginInvoke(asyncCallback, "异步调用返回值");
//endinvoke
int num1 = action.EndInvoke(asyncResult);
Console.WriteLine(num1);
}
}
public class BookShop
{
public int num = 1;
public void Sale()
{
lock (this)
{
int tmp = num;
if (tmp > 0)
{
Thread.Sleep(1000);
num = num - 1;
Console.WriteLine("售出一本图书,还剩{0}本", num);
}
else
{
Console.WriteLine("没有了");
}
}
}
}
}
C# 多线程
最新推荐文章于 2024-09-27 15:31:43 发布