美文网首页
多线程方法

多线程方法

作者: forsek | 来源:发表于2017-10-17 12:50 被阅读0次
一. 基础并行多线程结构
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();
        }
    }
}

相关文章

网友评论

      本文标题:多线程方法

      本文链接:https://www.haomeiwen.com/subject/duzruxtx.html