zoukankan      html  css  js  c++  java
  • 多线程笔记

    (一)线程同步
    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);
                }
            }
        }
    }

  • 相关阅读:
    Atitit alldiaryindex v1 t717 目录 1. Fix 1 2. Diary detail 1 2.1. Diary 1987---2016.12 1 2.2. Diary20
    Atitit web httphandler的实现 java python node.js c# net php 目录 1.1. Java 过滤器 servelet 1 1.2. Python的
    Atitit 网关协议cgi wsgi fcgi fastcgi 目录 1.1. CGI(common gateway unterface) 1 1.2. 2.1 WSGI: 1 1.3. 2.3
    Atitit gui的实现模式文本 dom ast 像素绘图api native 目录 1. Pl ast xml domAst 1 1.1. 简介 1 1.1.1. 【具体语法树】 2 2.
    Atitit 遍历文件夹目录解决方案与规范 attilax总结 1. 规范 2 1.1. 注意的不要同时改变文件夹内容,增删文件。这样获取到的目录list不会变化 2 1.2. 主义中文名称文件读写
    Atitit node.js自定义模块化 Function walkFileTree() exports.walkFileTree =walkFileTree 引用  Files=require
    Atitit node.js问题解决总结t99 目录 1.1. 找不到node程序 1 1.2. GBK编码问题 1 2. 按行读取gbk编码问题 2 2.1. 先写入txt utf8 encode
    Atitit io读取文件法 目录 1. 文件法 1 1.1. 异步读取文件: 1 1.2. 2.同步读取方法 1 1.3. 二进制读文件: 1 2. 读取api规范 1 3. Atitit 按照
    Atitit 按照行读取文件 目录 1.1. 类库'readline' 1 1.2. 类库C:workspacedataindexGenerNodejssdkioFileUtils.js
    Atitit sumdoc index 2019 v6 t56 .docx Atitit sumdoc index s99 目录 1. Zip ver 1 1.1. C:UsersAdminis
  • 原文地址:https://www.cnblogs.com/zpc870921/p/2640587.html
Copyright © 2011-2022 走看看