简单的多线程操作示例

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

                
                

            }

        }

    }

}

原文地址:https://www.cnblogs.com/furenjun/p/349208.html