日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多线程互斥实例

發(fā)布時(shí)間:2025/3/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多线程互斥实例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
??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????///?多線程互斥實(shí)例。
?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????????///?必需的設(shè)計(jì)器變量。
?20????????///?</summary>

?21????????private?System.ComponentModel.Container?components?=?null;
?22????????public?Form1()
?23????????{
?24????????????//?Windows?窗體設(shè)計(jì)器支持所必需的
?25????????????InitializeComponent();
?26????????????//?TODO:?在?InitializeComponent?調(diào)用后添加任何構(gòu)造函數(shù)代碼
?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????????///?設(shè)計(jì)器支持所需的方法?-?不要使用代碼編輯器修改
?46????????///?此方法的內(nèi)容。
?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(384,?216);
?59????????????this.textBox1.TabIndex?=?0;
?60????????????this.textBox1.Text?=?"";
?61????????????//?button1
?62????????????this.button1.Location?=?new?System.Drawing.Point(104,?232);
?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(208,?232);
?71????????????this.button2.Name?=?"button2";
?72????????????this.button2.TabIndex?=?2;
?73????????????this.button2.Text?=?"結(jié)束";
?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(8,?18);
?78????????????this.ClientSize?=?new?System.Drawing.Size(384,?264);
?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????????///?應(yīng)用程序的主入口點(diǎn)。
?94????????///?</summary>

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

100????????//?定義兩個(gè)線程變量。
101????????System.Threading.Thread?thdOne;
102????????System.Threading.Thread?thdTwo;
103????????//?定義一個(gè)線程運(yùn)行狀態(tài)變量。
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????????//?第一個(gè)線程。
116????????public?void?ThreadOne()
117????????{
118????????????while(true)
119????????????{
120????????????????System.Threading.Thread.Sleep(50);
121????????????????ShowChar('A');
122????????????}

123????????}

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

132????????}

133????????//?開始線程運(yùn)行。
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????????//??結(jié)束線程運(yùn)行。
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????????//?關(guān)閉窗體。
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

轉(zhuǎn)載于:https://www.cnblogs.com/gofficer/archive/2007/08/22/864891.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的多线程互斥实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。