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

  • 相关阅读:
    [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
    [Angular] Using ngTemplateOutlet to create dynamic template
    [Angular] Create dynamic content with <tempalte>
    [Postgres] Create a Postgres Table
    [Typescript 2] Nullable Types
    [Ramda] Sort, SortBy, SortWith in Ramda
    [Django] Creating an app, models and database
    .NET通用权限系统快速开发框架
    MVC中使用EF(1):为ASP.NET MVC程序创建Entity Framework数据模型
    TIME_WAIT引起Cannot assign requested address报错
  • 原文地址:https://www.cnblogs.com/xingchen/p/2035085.html
Copyright © 2011-2022 走看看