none
Main Thread Collapse(主线程崩溃,这是怎么引发的?) RRS feed

  • 问题

  • 以下是原代码:

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

    运行该程程,按下“btnStartA”按钮 主进程马上会结束,没有任何提示。

    2013年3月29日 8:15

全部回复