这里说说Thread,之前的文章中有BackgroundWorker的用法。他们都是线程,每一个用法在不同的场景使用不同。
Thread的控制非常的少,所以也就非常的简单,大部分使用都是开启后台线程,然后让后台跑,中间很少有操作,最后直接使用Abort关闭该线程即可。
1.首先看界面,启动2个线程,一个进行加法计算,一个进行减法计算。并且使界面不卡,随时可以拖动
2.在load方法中新建2个线程
3. 两个线程分别运行加法的方法A和减法的方法B
注意:这里需要使用Invoke,否则报错下图
4.效果如下
所有代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread t1;
Thread t2;
private void Form1_Load(object sender, EventArgs e)
{
t1 = new Thread(A);
t1.Start();
t2 = new Thread(B);
t2.Start();
}
public void A()
{
for (int i = 0; i < 20; i++)
{
Thread.Sleep(1000);
//label1.Text = i.ToString();
Invoke(new Action(() => label1.Text = i.ToString()));
}
}
public void B()
{
for (int i = 0; i < Convert.ToInt16(label2.Text); i++) //界面的值设置100
{
Thread.Sleep(1000);
Invoke(new Action(() => label2.Text = (Convert.ToInt16(label2.Text) - 1).ToString()));
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
t1.Abort();
t2.Abort(); //线程停止
}
}
}
拓展
Thread传递参数
有2种方法
第一种
传递一个object类型对象,比如像A方法中传递20
private void Form1_Load(object sender, EventArgs e)
{
t1 = new Thread(A);
t1.Start(20);
t2 = new Thread(B);
t2.Start();
}
public void A(object a)
{
for (int i = 0; i < Convert.ToInt32(a); i++)
{
Thread.Sleep(1000);
//label1.Text = i.ToString();
Invoke(new Action(() => label1.Text = i.ToString()));
}
}
第二种
使用拉姆达表达式,自定义传递参数,比如传递2个参数
private void Form1_Load(object sender, EventArgs e)
{
t1 = new Thread(A);
t1.Start(20);
//t2 = new Thread(B);
//t2.Start();
t2 = new Thread(()=>B("123",100));
t2.Start();
}
public void A(object a)
{
for (int i = 0; i < Convert.ToInt32(a); i++)
{
Thread.Sleep(1000);
//label1.Text = i.ToString();
Invoke(new Action(() => label1.Text = i.ToString()));
}
}
public void B(string a,object b)
{
for (int i = 0; i < Convert.ToInt16(b); i++) //界面的值设置100
{
Thread.Sleep(1000);
Invoke(new Action(() => label2.Text = (Convert.ToInt16(label2.Text) - 1).ToString()));
}
}
Thread返回值,和正常方法一样,可以返回任何类型的值
还是使用拉姆达表达式,传递2个参数,等待方法执行完成后,返回456
private void Form1_Load(object sender, EventArgs e)
{
t1 = new Thread(A);
t1.Start(20);
//t2 = new Thread(B);
//t2.Start();
//t2 = new Thread(()=>B("123",100));
//t2.Start();
t2 = new Thread(() =>
{
string s = B1("123", 10);
MessageBox.Show(s);
}
);
t2.Start();
}
public void A(object a)
{
for (int i = 0; i < Convert.ToInt32(a); i++)
{
Thread.Sleep(1000);
//label1.Text = i.ToString();
Invoke(new Action(() => label1.Text = i.ToString()));
}
}
public void B(string a, object b)
{
for (int i = 0; i < Convert.ToInt16(b); i++) //界面的值设置100
{
Thread.Sleep(1000);
Invoke(new Action(() => label2.Text = (Convert.ToInt16(label2.Text) - 1).ToString()));
}
}
public string B1(string a, object b)
{
for (int i = 0; i < Convert.ToInt16(b); i++) //界面的值设置100
{
Thread.Sleep(1000);
Invoke(new Action(() => label2.Text = (Convert.ToInt16(label2.Text) - 1).ToString()));
}
return "456";
}
拓展的所有代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread t1;
Thread t2;
private void Form1_Load(object sender, EventArgs e)
{
t1 = new Thread(A);
t1.Start(20);
//t2 = new Thread(B);
//t2.Start();
//t2 = new Thread(()=>B("123",100));
//t2.Start();
t2 = new Thread(() =>
{
string s = B1("123", 10);
MessageBox.Show(s);
}
);
t2.Start();
}
public void A(object a)
{
for (int i = 0; i < Convert.ToInt32(a); i++)
{
Thread.Sleep(1000);
//label1.Text = i.ToString();
Invoke(new Action(() => label1.Text = i.ToString()));
}
}
public void B(string a, object b)
{
for (int i = 0; i < Convert.ToInt16(b); i++) //界面的值设置100
{
Thread.Sleep(1000);
Invoke(new Action(() => label2.Text = (Convert.ToInt16(label2.Text) - 1).ToString()));
}
}
public string B1(string a, object b)
{
for (int i = 0; i < Convert.ToInt16(b); i++) //界面的值设置100
{
Thread.Sleep(1000);
Invoke(new Action(() => label2.Text = (Convert.ToInt16(label2.Text) - 1).ToString()));
}
return "456";
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
t1.Abort();
t2.Abort(); //线程停止
}
}
}
来源:c#中使用Thread_c# thread-CSDN博客