多线程笔记

(一)线程同步
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace _03_多线程
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //线程间操作无效: 从不是创建控件“textBox1”的线程访问它。
            //无法在一个线程中去操作另一个线程创建的控件
            Control.CheckForIllegalCrossThreadCalls = false;

            //定义了一个线程
            Thread th = new Thread(new ThreadStart(Test));
            th.Name = "th0";
            th.Start();

            Thread th1 = new Thread(Test);
            th1.Name = "th1";
            th1.Start();
        }

        object o = new object();
        //开辟内存空间
        //调用构造函数
        //存储同步块索引(默认负数)
        void Test()
        {
            for (int i = 0; i < 1000; i++)
            {
                //线程同步
                lock(o)
                {
                    int num = int.Parse(textBox1.Text);
                    num++;
                    textBox1.Text = num.ToString();
                    Console.WriteLine(Thread.CurrentThread.Name + "==" + num);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test");
        }
    }
}
(二)线程同步
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
namespace _04_前后台线程
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(Test);
            //后台线程
            //th.IsBackground = true;
            th.Start();

            MessageBox.Show("Test111");
        }

        void Test()
        {
            for (int i = 0; i < 100000; i++)
            {
                Console.WriteLine(i);
            }
        }

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

(三)摇奖机
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
namespace _05_摇奖机
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //不检查跨线程操作的合法性
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        //默认没有开始
        bool isStart = false;
        Thread th;
        private void button1_Click(object sender, EventArgs e)
        {
            //开始动态显示数字
            if (!isStart)
            {
                th = new Thread(Test);
                th.IsBackground = true;
                th.Start();

                isStart = true;
                button1.Text = "停止";
            }
            //停止动态显示数字
            else
            {
                isStart = false;
                button1.Text = "开始";
                th.Abort();
            }
        }
        void Test()
        {
            Random r = new Random();
            while (true)
            {
                //label中显示随机数字
                System.Threading.Thread.Sleep(100);
                label1.Text = r.Next(0, 10).ToString();
            }
        }
    }
}

(四)带参数的多线程
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
namespace _06_执行带参数的方法
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] names = { "蒋卫生","尤期间","马户"};

            Thread th = new Thread(Test);
            th.Start(names);
        }


        void Test(object o)
        {
            string[] arr = o as string[];
            foreach (string item in arr)
            {
                MessageBox.Show(item);
            }
        }
    }
}

原文地址:https://www.cnblogs.com/zpc870921/p/2640587.html