zoukankan      html  css  js  c++  java
  • C# 双线程

    转载:http://hi.baidu.com/tokc/blog/item/14e2366c6c321cf743169487.html

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading; //添加系统类库

    namespace testThread
    {
        public partial class Form1 : Form
        {
            Thread thread1 ; //创建线程thread1
            Thread thread2 ; //创建线程thread2
            int min = 0;
            int max = 1000;

            public Form1()
            {
                InitializeComponent();
                thread1 = new Thread(t1); //初始化线程thread1并使用自定义方法t1
                thread2 = new Thread(t2); //初始化线程thread2并使用自定义方法t2         
            }

            private void buttonStart_Click(object sender, EventArgs e) //开始按钮
            {
                string stringState = thread1.ThreadState.ToString();

                switch (stringState)
                {
                    case "Unstarted": //第一次启动
                        try
                        {
                            thread1.Start();
                            thread2.Start();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        break;
                    case "Running":    //正在运行,此状态删掉亦可
                        break;
                    case "Suspended": //挂起则恢复运行
                        thread1.Resume();
                        thread2.Resume();
                        break;
                    case "Stopped": //线程已停止则重新启动
                        thread1 = new Thread(t1);
                        thread2 = new Thread(t2);
                        thread1.Start();
                        thread2.Start();
                        break;
                    default: //什么都不做
                        break;
                }
            }

            private void buttonSuspend_Click(object sender, EventArgs e) //挂起按钮
            {
                try //挂起线程
                {
                    thread1.Suspend();
                    thread2.Suspend();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            private void button3_Click(object sender, EventArgs e) //关闭按钮
            {
                this.Close(); // 关闭窗口
            }

            public void t1()
            {
                progressBar1.Minimum = min; //进程滚动条1最小值
                progressBar1.Maximum = max; //进程滚动条2最大值
                for (int i = min; i < max; i++)
                {
                    label1.Text = i.ToString(); //把i值显示在label1控件上
                    progressBar1.Value = i; //把i值赋予滚动条当前值
                    Thread.Sleep(10); //线程休息10毫秒
                }
            }

            public void t2() //看t1
            {
                progressBar2.Minimum = min;
                progressBar2.Maximum = max;
                for (int i = min; i < max; i++)
                {
                    label2.Text = i.ToString();
                    progressBar2.Value = i;
                    Thread.Sleep(10);
                }
            }        
        }
    }

  • 相关阅读:
    「酷客多」关注:马化腾公开演讲,透露2017年春节前会推出“小程序”
    微信小程序购物商城系统开发系列-目录结构
    微信小程序购物商城系统开发系列-工具篇
    上海闪酷成为京东商城第一批独立软件开发商(ISV)
    【FFMPEG】关于硬解码和软解码
    Git 别名配置
    【Linux】扩展阿里云数据盘分区和文件系统
    Python实现MQTT接收订阅数据
    【Linux】Devops的一些运维工具
    【Linux】YUM Repositories for CentOS, RHEL & Fedora Systems
  • 原文地址:https://www.cnblogs.com/xingchen/p/2035085.html
Copyright © 2011-2022 走看看