Main Thread Collapse(主线程崩溃,这是怎么引发的?)
public class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
#region InitializeComponent
/// <summary>
/// ¥ナ←ワタレト│ᆴᄒ│ᆴᄀ¥ルᄄ¥マリ←ヌマ ̄タツ
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// ₩ᄌナミニ₩ノタ₩ワノ₩ᆳᆪ¥ワᄄ¦ᄑヤᄄレト│ᄉト₩ᄎミ ̄タツ
/// </summary>
/// <param name="disposing">¥ᆭツ₩゙ワ¥ᄎヤ←ヌハ₩ヤᄒ₩ノリᆴᄀ│ᄉト₩ᄎミᄐフ¦ᄌᄎ trueᄐロ¥ミᆭ¥ネル¦ᄌᄎ false ̄タツ</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.btnStartA = new System.Windows.Forms.Button();
//
// btnStart
//
this.btnStartA.AccessibleDescription = "01";
this.btnStartA.Location = new System.Drawing.Point(428, 94);
this.btnStartA.Name = "btnStartA";
this.btnStartA.Size = new System.Drawing.Size(75, 23);
this.btnStartA.TabIndex = 0;
this.btnStartA.Text = "¥ᄐタ¥ᄃヒA";
this.btnStartA.UseVisualStyleBackColor = true;
this.btnStartA.Click += new System.EventHandler(this.btnStartA_Click);
//
// FrmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(572, 344);
this.Controls.Add(this.btnStartA);
this.Name = "FrmMain";
this.Text = "Form1";
this.ResumeLayout(false);
}
private System.Windows.Forms.Button btnStartA;
#endregion
public const int ThreadNumber = 10;
AutoResetEvent[] stopThreadEve;
private void btnStartA_Click(object sender, EventArgs e)
{
stopThreadEve = new AutoResetEvent[ThreadNumber];
for (int i = 0; i < ThreadNumber; i++)
{
ThreadParas para = new ThreadParas();
AutoResetEvent tempEve = new AutoResetEvent(false);
para.EvnFlag = tempEve;
para.Timeout = 5000;
this.stopThreadEve[i] = tempEve;
System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(this.TestThreadMethodA), para);
}
}
private void TestThreadMethodA(object obj)
{
if (obj is ThreadParas)
{
ThreadParas para = obj as ThreadParas;
while (true)
{
if (para.EvnFlag.WaitOne(para.Timeout))
{
break;
}
}
}
}
}
public class ThreadParas
{
private int timeout;
private System.Threading.AutoResetEvent evnFlag;
public int Timeout
{
get { return timeout; }
set { timeout = value; }
}
public System.Threading.AutoResetEvent EvnFlag
{
get { return evnFlag; }
set { evnFlag = value; }
}
}
dragoon
10 years, 10 months ago