c#中使用Thread

这里说说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博客

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故里2130

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值