C#如何使用线程

C#如何使用线程

实现的效果:

第一步:创建一个窗体类(参考前期文章)并加入button类、label类等(如上图)

第二步:创建主线程以及label1显示的效果

第三步:创建线程任务并进行上锁(避免后续出现错误)

第四步:对几个按钮进行逻辑设定

private void button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        int index = i;
        thread = new Thread(() =>
        {
            GrabTicket();
        });
        thread.Name = "ID"+index.ToString();
        thread.Start();
    }
}
private void button2_Click(object sender, EventArgs e)
{
    label1.Text = "当前票数:>" + ticket.ToString();
}
Thread thread;
bool flag = true;
private void button3_Click(object sender, EventArgs e)
{
    flag = false;
    thread.Abort();
}
private void button4_Click(object sender, EventArgs e)
{
    Console.WriteLine(Thread.CurrentThread.Name+"当前ID;"+Thread.CurrentThread.ManagedThreadId);
    int a = 0;
    thread = new Thread(() =>
    {
        //Console.WriteLine("当前名称" + Thread.CurrentThread.Name + " 当前ID:" + Thread.CurrentThread.ManagedThreadId);
        //直接写要执行的逻辑
        while (flag)
        {
            a++;
            //this.InvokeRequired 判断 当前线程 和我this(Form 主线程)是否线程安全
            //如果 this.InvokeRequired 为true证明不安全 如果为false证明安全
            if (this.InvokeRequired)
            {
                //委托主线程帮忙修改
                this.Invoke(new Action(() =>
                {
                    //Console.WriteLine("当前名称" + Thread.CurrentThread.Name + " 当前ID:" + Thread.CurrentThread.ManagedThreadId);
                    label3.Text = a.ToString();
                }));
                //睡眠放到主线程里面会造成阻塞
                Thread.Sleep(200);
            }
            else
            {
                label3.Text = a.ToString();
                Thread.Sleep(500);
            }

        }
    });
    thread.Name = "我是子线程";
    thread.Start();
}

第五步:继续创建另一种运行线程

void Run(int ID)
{
    Console.WriteLine("ID"+ID);
}
Thread threadTest;
private void button5_Click(object sender, EventArgs e)
{
    for (int i = 0; i <= 20; i++)
    {
        int index = i;
        Thread threadTest = new Thread(() =>
        {
            Run(index);
        });
        threadTest.Start();
    }
}

完整代码如下:

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 _20240829孙光辉
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Text = "当前票数:>" + ticket.ToString();
            Thread.CurrentThread.Name = "我是主线程";
        }
        int ticket = 2;
        object suo = new object();
        void GrabTicket()
        {
            lock (suo)
            {
                if (ticket > 0)
                {
                    Thread.Sleep(1000);
                    ticket--;
                    Console.WriteLine(Thread.CurrentThread.Name + "抢票成功,当前票数:>" + ticket);
                }
                else
                {
                    Console.WriteLine(Thread.CurrentThread.Name + "抢票失败");
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                int index = i;
                thread = new Thread(() =>
                {
                    GrabTicket();
                });
                thread.Name = "ID"+index.ToString();
                thread.Start();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            label1.Text = "当前票数:>" + ticket.ToString();
        }
        Thread thread;
        bool flag = true;
        private void button3_Click(object sender, EventArgs e)
        {
            flag = false;
            thread.Abort();
        }
        private void button4_Click(object sender, EventArgs e)
        {
            Console.WriteLine(Thread.CurrentThread.Name+"当前ID;"+Thread.CurrentThread.ManagedThreadId);
            int a = 0;
            thread = new Thread(() =>
            {
                //Console.WriteLine("当前名称" + Thread.CurrentThread.Name + " 当前ID:" + Thread.CurrentThread.ManagedThreadId);
                //直接写要执行的逻辑
                while (flag)
                {
                    a++;
                    //this.InvokeRequired 判断 当前线程 和我this(Form 主线程)是否线程安全
                    //如果 this.InvokeRequired 为true证明不安全 如果为false证明安全
                    if (this.InvokeRequired)
                    {
                        //委托主线程帮忙修改
                        this.Invoke(new Action(() =>
                        {
                            //Console.WriteLine("当前名称" + Thread.CurrentThread.Name + " 当前ID:" + Thread.CurrentThread.ManagedThreadId);
                            label3.Text = a.ToString();
                        }));
                        //睡眠放到主线程里面会造成阻塞
                        Thread.Sleep(200);
                    }
                    else
                    {
                        label3.Text = a.ToString();
                        Thread.Sleep(500);
                    }

                }
            });
            thread.Name = "我是子线程";
            thread.Start();
        }
        void Run(int ID)
        {
            Console.WriteLine("ID"+ID);
        }
        Thread threadTest;
        private void button5_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= 20; i++)
            {
                int index = i;
                Thread threadTest = new Thread(() =>
                {
                    Run(index);
                });
                threadTest.Start();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值