多线程应用

多线程初始

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 _01_多线程演示
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Thread thread =Thread.CurrentThread;
            thread.Name = "主";
        }
        private void Tzm()
        {
            Console.WriteLine($"当前任务执行在{Thread.CurrentThread.Name}线程");

            Thread.Sleep(5000);//
            MessageBox.Show("芝麻挑好了,滚!!!!!");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Tzm();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("给");
        }

        private void button2_Click(object sender, EventArgs e)
        {

            //用分线程执行耗时任务

            //1.创建一个委托,用于告诉线程要干什么工作
            ThreadStart threadStart = new ThreadStart(Tzm);


            //2.创建一个线程,并告知其任务
            Thread t= new Thread(threadStart);
            t.Name = "分";
            //3.启动线程开始执行任务
            t.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 _02_多线程案例
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Thread.CurrentThread.Name = "主";
        }

        void Calc()
        {
            Console.WriteLine($"当前任务执行在{Thread.CurrentThread.Name}线程");
            int sum = 0;
            for ( int i = 0; i <= 500; i++)
            {
                Thread.Sleep(1);
                sum += i;
            }

            // 需要使用启动并调试查看错误

            //System.InvalidOperationException:“线程间操作无效: 从不是创建控件“label1”的线程访问它。”
            
            //因为lable 控件是由主线程创建的,分线程不能去操作他


            //分线程不能操作UI(窗体和控件)
            label1.Text = $"1-500之间的数字的和是{sum}";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Calc();
        }

        private void button2_Click(object sender, EventArgs e)
        {
          
            ThreadStart threadStart = new ThreadStart(Calc);
            Thread t = new Thread(threadStart);
            t.Name = "分";
            t.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 _03_线程通讯_分传主
{
    public partial class Form1 : Form
    {
        //1.定义一个变量存储主线程的执行期上下文(作用域)
        SynchronizationContext MainCount;
        public Form1()
        {
            InitializeComponent();
            Thread.CurrentThread.Name = "主线程";
        }
        void Fn()
        {
            Console.WriteLine(Thread.CurrentThread.Name);
            Thread.Sleep(3000);
            //计算的结果: 吴亦凡






            //lable的赋值操作,必须在主线程,计算的结果在分线程,需要把分线程计算的结果传递给主线程
            // label1.Text = "吴亦凡";
            // 4.分线程调用方法,给主线程传递数据
            MainCount.Post(Abc,"吴亦凡");
        }

        private void button1_Click(object sender, EventArgs e)
        {


            //2.设置变量存储当前主线程执行期上下文

            MainCount = SynchronizationContext.Current;


            //3.启动分线程让他去执行耗时任务
            //ThreadStart threadStart = new ThreadStart(Fn);
            //Thread t = new Thread(threadStart);
            //t.Name = "分线程";
            //t.Start();
            //简写 省略委托
            new Thread(Fn) { Name = "分线程" }.Start();





        }
        //5.定义函数,当分线程发送数据的时候会执行这个函数
        void Abc(object o)
        {
            Console.WriteLine(o);//吴亦凡
            Console.WriteLine(Thread.CurrentThread.Name);//主线程
            label1.Text=o.ToString();
        }
    }
}

线程通讯_主传分

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 _04_线程通讯_主传分
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        void Calc(object obj)
        {
            int sum = 0;
            for (int i = 0; i < Convert.ToInt32(obj); i++)
            {
                Thread.Sleep(1);
                sum += i;
            }
            //分线程  sum 需要显示在ui界面上
            //默认情况下,分线程不能操作ui,需要把sum传递给主线程

            //在分线程中操作ui界面
            //参数是一个委托类型,也及时一个函数,函数里面就是需要在主线程干的事情
            Invoke(new Action(() =>
            {
                label1.Text = $"1-{obj}的和为{sum}";
            }));

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //主线程
            //Calc(100);
            //ThreadStart 没有参数的函数数据类型
            // ThreadStart threadStart = new ThreadStart(Calc);

            //创建一个有参数的委托数据类型
            //ParameterizedThreadStart ts=  new ParameterizedThreadStart(Calc);
            //Thread t = new Thread(ts);
            //t.Name = "分线程";
            在Start() 方法中传递参数即可
            //t.Start(100);

            new Thread(Calc).Start(100);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Calc(200);
            new Thread(Calc).Start(200);
        }
    }
}

关闭线程

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 _05_关闭线程
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void Fn()
        {
            Thread.Sleep(1000);
            Console.WriteLine("1秒过去了");
            Thread.Sleep(1000);
            Console.WriteLine("2秒过去了");
            Thread.Sleep(1000);
            Console.WriteLine("3秒过去了");
            Thread.Sleep(1000);
            Console.WriteLine("4秒过去了");
        }
        Thread th;
        private void button1_Click(object sender, EventArgs e)
        {
             th=  new Thread(Fn);
            th.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Abort(); 销毁线程
            //线程会在他任务执行完成之后被自动销毁,也可以手动销毁
            th.Abort();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值