zoukankan      html  css  js  c++  java
  • 简单的多线程操作示例

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading; 

    namespace 多线程操作
    {
        
    /// <summary>
        
    /// Form1 的摘要说明。
        
    /// </summary>

        public class Form1 : System.Windows.Forms.Form
        
    {
            
    private System.Windows.Forms.Panel panel1;
            
    private System.Windows.Forms.StatusBar statusBar1;
            
    private System.Windows.Forms.Panel panel2;
            
    private System.Windows.Forms.Button button1;
            
    private System.Windows.Forms.RichTextBox myRichTextBox;
            
    private System.Windows.Forms.Button button2;
            
    private System.Windows.Forms.Button button3;
            
    private System.Windows.Forms.Button button4;
            
    private System.Windows.Forms.Button button5;
            
    private System.Windows.Forms.Button button6;
            
    /// <summary>
            
    /// 必需的设计器变量。
            
    /// </summary>

            private System.ComponentModel.Container components = null;

            
    public Form1()
            
    {
                
    //
                
    // Windows 窗体设计器支持所必需的
                
    //
                InitializeComponent();

                
    //
                
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
                
    //
            }


            
    /// <summary>
            
    /// 清理所有正在使用的资源。
            
    /// </summary>

            protected override void Dispose( bool disposing )
            
    {
                
    if( disposing )
                
    {
                    
    if (components != null
                    
    {
                        components.Dispose();
                    }

                }

                
    base.Dispose( disposing );
            }


            
    Windows 窗体设计器生成的代码

            
    /// <summary>
            
    /// 应用程序的主入口点。
            
    /// </summary>

            [STAThread]
            
    static void Main() 
            
    {
                Application.Run(
    new Form1());
            }


            
    private void Form1_Load(object sender, System.EventArgs e)
            
    {
            
            }

            # region 
    "button1"
            
    //public static void Thread1() 
            public  void Thread1() 
            
    {
                
    for (int i = 0; i < 10; i++
                
    {
                    
    this.myRichTextBox.Text +=String.Format("\nThread1 {0} ", i);
                }

            }


            
    //public static void Thread2() 
            public  void Thread2() 
            
    {
                
    for (int i = 0; i < 10; i++
                
    {
                    
    this.myRichTextBox.Text +=String.Format("\nThread2 {0}", i);
                }

            }


            
            
    private void button1_Click(object sender, System.EventArgs e)
            
    {
            
                
    this.myRichTextBox.Text ="";
                Thread tid1 
    = new Thread(new ThreadStart(Thread1 ) );
                Thread tid2 
    = new Thread(new ThreadStart(Thread2 ) );

                tid1.Start();
                tid2.Start();
            }


            
    #endregion
            
    "button2"



        
    public class MyThread 
        
    {

            
    public static void Thread1() 
            
    {
                
    for (int i = 0; i < 10; i++
                
    {
                    Console.WriteLine(
    "Thread1 {0}", i);
                }

            }


            
    public static void Thread2() 
            
    {
                
    for (int i = 0; i < 10; i++
                
    {
                    Console.WriteLine(
    "Thread2 {0}", i);
                }

            }

            
    public static int CurrentRun=0;

            
    public static int gsCr
            
    {
                
    get{return CurrentRun;}
                
    set{CurrentRun=value;}
            }


            
    public  void Thread3() 
            
    {
                 gsCr
    =0;
                
    for (int i = 0; i < 10; i++
                
    {

                    Thread thr 
    = Thread.CurrentThread;
                    Console.WriteLine(thr.Name 
    + "=" + i+ "当前的数据: "+gsCr.ToString() );
                    gsCr
    =i;
                    
    try
                    
    {
                        
    //Thread.Sleep(1) ;  //休眠一秒钟
                        int iHour = 0;
                        
    int iMin = 0;
                        
    int iSec = 1//-1
                        Thread.Sleep(new TimeSpan(iHour, iMin, iSec) );
                    }

                    
    catch (ArgumentException ae) 
                    
    {
                        Console.WriteLine(ae.ToString() );
                    }

                    
    catch (ThreadInterruptedException tie) 
                    
    {
                        Console.WriteLine(tie.ToString() );
                    }

                    
                    

                }

            }

        }

    }

  • 相关阅读:
    Git使用感悟
    Just open sourced the JavaScript Inlay Hints Provider function in IDEA plugin React Native Console commercial version
    Flutter和React Native组件层级对比
    React Native 迁移到 Flutter 心得体会[持续更新中]
    IntelliJ IDEA 2020.3 EAP 新特性: 拖动分割编辑器,新欢迎窗口等
    Universal Abstract Syntax Tree (UAST)
    Creating your first Flutter project using WebStorm 2020
    WIN10系统跑流量,彻底杜绝win10更新程序后台下载
    linux centos7安装mysql5.7(安装版)教程
    VMware启动虚拟机一直处于黑屏且无反应
  • 原文地址:https://www.cnblogs.com/furenjun/p/349208.html
Copyright © 2011-2022 走看看