• 简单的多线程操作示例


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

                    
                    

                }

            }

        }

    }

  • 相关阅读:
    APP性能测试中的几个重要概念
    linux初学体会
    Centos 关闭指定端口的进程
    【初学Python】01-第一个小说爬虫
    SQLsever存储过程分页查询
    记MVC学习过程中一次传参到View时遇到的错误
    未能加载文件或程序集“System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项
    Android Studio导包无效,全部报红
    记一次使用SimpleDateFormat 格式化时间时遇到的问题
    Spring-Boot中Tomcat端口修改
  • 原文地址:https://www.cnblogs.com/furenjun/p/349208.html
Copyright © 2020-2023  润新知