• 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);
                }
            }        
        }
    }

  • 相关阅读:
    Jquery Ajax 调用 WebService
    Dapper.NET
    HTML5 canvas标签
    SQL内外左右交叉连接
    水晶报表纵向重复
    AngularJS 菜鸟
    什么是MVC框架?
    伪类和伪元素的区别
    常用的本地存储-----cookie篇
    JavaScript中基本数据类型和引用数据类型的区别
  • 原文地址:https://www.cnblogs.com/xingchen/p/2035085.html
Copyright © 2020-2023  润新知