多线程方法

一. 基础并行多线程结构
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.Collections.Concurrent;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace 多线程练习
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //建立大类的变量,用来在小类之间传递参数
        //用户登录SID
        private string m_ssid;
        //private string ssid_;//第二种写法
        private string m_yzmpic;
        private string m_yzmid;
        private string m_yzmidl;
        //角色ID
        private string m_charid;
        //循环次数
        private string m_cycle;
        //土地编号
        private string m_landnum;
        //用户名
        private string m_username;
        //密码
        private string m_password;
        //循环延时1,2,3
        private int delay1=200;
        private int delay2=1500;
        //显示延时类

        private void button1_Click(object sender, EventArgs e)
        {

            stopWatch.Start();//用来记录程序运行时间
            try
            {
                //Parallel.Invoke(Run1,Run2,Run3,Run4,Run5,Run6);//跑6个,注意需要在下方添加至少6个子程序。
                //Parallel.Invoke(Run1,Run2,Run3,Run4);//只跑两个线程
                Parallel.Invoke(Run1);//只跑一个
            }
            catch (AggregateException aex)
            {
                foreach (var ex in aex.InnerExceptions)
                {
                   MessageBox.Show(ex.Message);
                }
            }
            stopWatch.Stop();
                    MessageBox.Show("循环完成,总时间" + stopWatch.ElapsedMilliseconds/3600000.0 + "小时");//显示程序运行时间
        }
         private Stopwatch stopWatch = new Stopwatch();
        
        //加载界面时需要显示或进行的操作
         private void Form1_Load_1(object sender, EventArgs e)
         {
             label14.Text = delay1.ToString();
             label15.Text = delay1.ToString();
             label16.Text = delay2.ToString();
         }

        //并行子线程,可添加至n个,注意子程序间操作要互相独立,防止冲突。
          public void run1()
            {
                //需要执行的操作1
            }
          public void run2()
            {
                //需要执行的操作1或2
            }
          public void run3()
            {
                //需要执行的操作1或2或3
            }

    }
}
二. 标准多线程方法,控制并发线程数,带线程锁
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace WindowsFormsApplication2练习用
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    private Stopwatch stopWatch = new Stopwatch();

        private void buttonmt_Click(object sender, EventArgs e)
        {
            stopWatch.Start();
            int runCount = 0;//控制线程数变量
                
                //防止界面卡死的新线程
                new Thread(new ParameterizedThreadStart(delegate
                {   //多线程循环
                    for (int i = 0; i < 10; i++)
                    {
                        while (true)
                        {    //线程数控制
                            if (runCount < 3)
                            {
                                break;
                            }
                            else
                            {
                                Thread.Sleep(2000);
                                continue;
                            }
                        }
                        runCount++;
                        //多线程循环开始
                        new Thread(new ParameterizedThreadStart(delegate
                        {    //线程内操作(空),与子线程调用界面显示方法
                            BeginInvoke(new EventHandler(delegate
                            {
                                listBox1.Items.Add("线程已完成");
                            }));
                            lock (this)
                            {
                                runCount--;
                            }
                        })).Start();
                    }
                    //MessageBox.Show("程序已完成");
                    stopWatch.Stop();
                    //Thread.Sleep(5000);
                    MessageBox.Show("循环完成,总时间" + stopWatch.ElapsedMilliseconds + "毫秒");
                })).Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            stopWatch.Start();
            int runCount = 0;
            int[] intArray = { 1, 2, 3,4,5,6,7,8,9,0 };

            new Thread(new ParameterizedThreadStart(delegate
            {    //多线程遍历
                foreach (int i in intArray)
                {
                    while (true)
                    {
                        if (runCount < 2)
                        {
                            break;
                        }
                        else
                        {
                            Thread.Sleep(2000);
                            continue;
                        }
                    }
                    runCount++;
                    new Thread(new ParameterizedThreadStart(delegate
                    {
                        BeginInvoke(new EventHandler(delegate
                        {
                            listBox1.Items.Add("线程已完成"+intArray[i]);
                        }));
                        lock (this)
                        {
                            runCount--;
                        }
                    })).Start();                   
                }
                stopWatch.Stop();
                MessageBox.Show("循环完成,总时间" + stopWatch.ElapsedMilliseconds + "毫秒");
            })).Start();
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,425评论 11 349
  • Object C中创建线程的方法是什么?如果在主线程中执行代码,方法是什么?如果想延时执行代码、方法又是什么? 1...
    AlanGe阅读 5,773评论 0 17
  • Java8张图 11、字符串不变性 12、equals()方法、hashCode()方法的区别 13、...
    Miley_MOJIE阅读 9,118评论 0 11
  • 追女大神的秘诀## 读书时,淘爸认识一位冷兄,此人非官二代和富二代,也非具备异于常人的超能力。他大学期间谈了8个女...
    淘爸阅读 3,658评论 4 6
  • 几天前,来到简书,愈发觉得喜欢她了。 第一次在简书写东西,且是写一篇这样的东西,心里难免有点紧张,毕竟我也不是个很...
    王贰贰阅读 3,921评论 1 5