多线程互斥实例

本文介绍了一个简单的多线程互斥示例,通过两个线程交替向文本框中写入字符A和B,演示了如何使用锁来避免线程间的冲突。当一个线程在写入时,另一个线程必须等待。
  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.Windows.Forms;
  6using System.Data;
  7
  8namespace ThreadMutex
  9{
 10    /**//// <summary>
 11    /// 多线程互斥实例。
 12    /// </summary>

 13    public class Form1 : System.Windows.Forms.Form
 14    {
 15        private System.Windows.Forms.TextBox textBox1;
 16        private System.Windows.Forms.Button button1;
 17        private System.Windows.Forms.Button button2;
 18        /**//// <summary>
 19        /// 必需的设计器变量。
 20        /// </summary>

 21        private System.ComponentModel.Container components = null;
 22        public Form1()
 23        {
 24            // Windows 窗体设计器支持所必需的
 25            InitializeComponent();
 26            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 27        }

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

 31        protected override void Dispose( bool disposing )
 32        {
 33            if( disposing )
 34            {
 35                if (components != null)
 36                {
 37                    components.Dispose();
 38                }

 39            }

 40            base.Dispose( disposing );
 41        }

 42
 43        Windows Form Designer generated code#region Windows Form Designer generated code
 44        /**//// <summary>
 45        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 46        /// 此方法的内容。
 47        /// </summary>

 48        private void InitializeComponent()
 49        {
 50            this.textBox1 = new System.Windows.Forms.TextBox();
 51            this.button1 = new System.Windows.Forms.Button();
 52            this.button2 = new System.Windows.Forms.Button();
 53            this.SuspendLayout();
 54            // textBox1
 55            this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
 56            this.textBox1.Multiline = true;
 57            this.textBox1.Name = "textBox1";
 58            this.textBox1.Size = new System.Drawing.Size(384216);
 59            this.textBox1.TabIndex = 0;
 60            this.textBox1.Text = "";
 61            // button1
 62            this.button1.Location = new System.Drawing.Point(104232);
 63            this.button1.Name = "button1";
 64            this.button1.TabIndex = 1;
 65            this.button1.Text = "开始";
 66            this.button1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
 67            this.button1.Click += new System.EventHandler(this.button1_Click);
 68            // button2
 69            this.button2.Enabled = false;
 70            this.button2.Location = new System.Drawing.Point(208232);
 71            this.button2.Name = "button2";
 72            this.button2.TabIndex = 2;
 73            this.button2.Text = "结束";
 74            this.button2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
 75            this.button2.Click += new System.EventHandler(this.button2_Click);
 76            // Form1
 77            this.AutoScaleBaseSize = new System.Drawing.Size(818);
 78            this.ClientSize = new System.Drawing.Size(384264);
 79            this.Controls.AddRange(new System.Windows.Forms.Control[] {
 80                  this.button2,
 81                  this.button1,
 82                  this.textBox1}
);
 83            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
 84            this.Name = "Form1";
 85            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
 86            this.Text = "多线程互斥";
 87            this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
 88            this.ResumeLayout(false);
 89
 90        }

 91        #endregion

 92        /**//// <summary>
 93        /// 应用程序的主入口点。
 94        /// </summary>

 95        [STAThread]
 96        static void Main()
 97        {
 98            Application.Run(new Form1());
 99        }

100        // 定义两个线程变量。
101        System.Threading.Thread thdOne;
102        System.Threading.Thread thdTwo;
103        // 定义一个线程运行状态变量。
104        public const int START = 1;
105        public const int STOP  = 2;
106        public int m_iRun = STOP;
107        // 输出显示字符。
108        public void ShowChar(char ch)
109        {
110            lock(this)
111            {
112                textBox1.Text += ch;
113            }

114        }

115        // 第一个线程。
116        public void ThreadOne()
117        {
118            while(true)
119            {
120                System.Threading.Thread.Sleep(50);
121                ShowChar('A');
122            }

123        }

124        // 第二个线程。
125        public void ThreadTwo()
126        {
127            while(true)
128            {
129                System.Threading.Thread.Sleep(25);
130                ShowChar('B');
131            }

132        }

133        // 开始线程运行。
134        private void button1_Click(object sender, System.EventArgs e)
135        {
136            textBox1.Text = "";
137            thdOne = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadOne));
138            thdTwo = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadTwo));
139            thdOne.Start();
140            thdTwo.Start();
141            button1.Enabled = false;
142            button2.Enabled = true;
143            m_iRun = START;
144        }

145        //  结束线程运行。
146        private void button2_Click(object sender, System.EventArgs e)
147        {
148            button1.Enabled = true;
149            button2.Enabled = false;
150            m_iRun = STOP;
151            if (thdOne != null)
152                thdOne.Abort();
153            if (thdTwo != null)
154                thdTwo.Abort();
155        }

156        // 关闭窗体。
157        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
158        {
159            if (m_iRun == START)
160            {
161                m_iRun = STOP;
162                if (thdOne != null)
163                    thdOne.Abort();
164                if (thdTwo != null)
165                    thdTwo.Abort();
166            }

167        }

168    }

169}

170

转载于:https://www.cnblogs.com/gofficer/archive/2007/08/22/864891.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值