线程的创建与销毁

线程管理

在System.Threading命名空间下的Thread类,用于对线程进行管理。



一、Thread类的常用属性和方法

属性和方法说明
CurrentThread获取当前正在运行的进程
ThreadState获取一个值,该值包含当前线程的状态
IsAlive获取指示当前线程的执行状态的值
Start()启动当前线程
Sleep()将当前线程挂起指定的毫秒数
Abort()销毁线程
Interrupt()中断处于Wait、Sleep、Join线程状态的线程

二、创建和启动线程

1.创建线程

创建线程时,需要提供线程入口,即该线程执行什么方法。语法格式如下:
使用 ThreadStart 委托:

Thread t = new Thread(new ThreadStart(method));

直接传递方法:

Thread tr = new Thread(method);

区别

语法简洁性:

直接传递方法的形式更加简洁,减少了代码量。
使用 ThreadStart 委托的形式更明确地指出了方法的类型,增加了代码的可读性。

适用性:

直接传递方法的形式适用于 C# 7.0 及更高版本。
使用 ThreadStart 委托的形式在早期版本的 C# 中同样适用。

2.启动线程

Start()方法用于启动一个线程,语法格式如下:

t.Start();

3.线程创建和使用

界面如下所示:
在这里插入图片描述

方法1

using System.Threading;
using System.Windows.Forms;

namespace process2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(method));
            //Thread t = new Thread(method);
            t.Start();
        }
        private void method()
        {
            for(int i = 1;i <= 100; i++)
            {
                if(i % 10 == 0)
                { 
                    rtb_process.AppendText(i.ToString() + "\r\n");                                     
                }
                else
                {
                    rtb_process.AppendText(i.ToString()+" ");                    
                }
            }
        }               
    }
}

方法2

using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;

namespace process2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ParameterizedThreadStart(method));
            t.Start(this.rtb_process);
        }

        public static void method(object state)
        {
            RichTextBox richTextBox = state as RichTextBox;
            for (int i = 1; i <= 100; i++)
            {
                if (i % 10 == 0)
                {
                    richTextBox.Invoke((MethodInvoker)delegate
                    {
                        richTextBox.AppendText (i.ToString()+"\r\n");
                    });
                }
                else
                {
                    richTextBox.AppendText(i.ToString()+" ");
                }
            }
        }
    }
}

方法3

using System.Windows.Forms;
using System.Threading;

namespace process2
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread backgroundThread = new Thread(() => method(this.rtb_process));
            backgroundThread.Start();
        }
        public static void method(RichTextBox richTextBox)
        {
            for (int i = 1; i <= 100; i++)
            {
                if (i % 10 == 0)
                {
                    richTextBox.Invoke((MethodInvoker)delegate
                    {
                        richTextBox.AppendText(i.ToString() + "\r\n");
                    });

                }
                else
                {
                    richTextBox.Invoke((MethodInvoker)delegate
                    {
                        richTextBox.AppendText(i.ToString() + " ");
                    });
                }
            }
        }
    }
}

运行结果

在这里插入图片描述

4.线程休眠

Thread.Sleep(休眠时间);

using System.Windows.Forms;
using System.Threading;

namespace process2
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void method()
        {
            rtb_sleep.AppendText("当前线程状态:"+ Thread.CurrentThread.ThreadState.ToString());
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(method);
            thread.Start();
            while(thread.IsAlive)
            {
                rtb_sleep.AppendText("线程开始执行");
                Thread.Sleep(5);
            }
        }
    }
}

4.终止和销毁线程

终止线程语法:Thread.Interrupt();
销毁线程语法:if(thread.IsAlive) { thread.Abort(); }

using System.Threading;
using System.Windows.Forms;

namespace process2
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }
        private void method()
        {
            for (int i = 1; i <3000;i++)
            {
                if(i % 60 == 0)
                {
                    richTextBox1.AppendText("*");
                }
                else
                {
                    richTextBox1.AppendText("*");
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(method));
            richTextBox1.AppendText("线程开始启动");
            thread.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(method));
            Thread.Sleep(1000);
            if(thread.IsAlive)
            {
                thread.Abort();
            }
            richTextBox1.AppendText("线程被销毁");
        }
    }
}

总结

`以上代码都可以正常运行,但是使用委托那边还是有点蒙的,不是很明白为什么。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值