点start启动程序,但是点pause不会挂起,然后查看primeNumberThread的状态发现是WaitSleepJoin
为什么运行中的线程会是"等待睡眠连接"啊

解决方案 »

  1.   

    代码如下
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading;
    using System.Text;
    namespace WindowsApplication1
    {
    public class Form1 : System.Windows.Forms.Form
    { private System.ComponentModel.Container components = null;
    private System.Windows.Forms.ListBox lstPrime;
    private System.Windows.Forms.Button cmdPause;
    private System.Windows.Forms.Button cmdStart;
    private System.Windows.Forms.Button cmdResume;
    private System.Windows.Forms.Button cmdStop;
    private delegate void UpdateValue(string returnValue);
    private delegate void UpdateState(string returnState);
    private Thread t = null;
    private Thread primeNumberThread;
    public Form1()
    { InitializeComponent(); }
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    private void InitializeComponent()
    {
    this.lstPrime = new System.Windows.Forms.ListBox();
    this.cmdStart = new System.Windows.Forms.Button();
    this.cmdPause = new System.Windows.Forms.Button();
    this.cmdResume = new System.Windows.Forms.Button();
    this.cmdStop = new System.Windows.Forms.Button();
    this.SuspendLayout();
    this.lstPrime.ColumnWidth = 100;
    this.lstPrime.ItemHeight = 12;
    this.lstPrime.Location = new System.Drawing.Point(64, 40);
    this.lstPrime.MultiColumn = true;
    this.lstPrime.Name = "lstPrime";
    this.lstPrime.Size = new System.Drawing.Size(552, 376);
    this.lstPrime.TabIndex = 0;
    this.cmdStart.Location = new System.Drawing.Point(120, 440);
    this.cmdStart.Name = "cmdStart";
    this.cmdStart.TabIndex = 1;
    this.cmdStart.Text = "Start";
    this.cmdStart.Click += new System.EventHandler(this.cmdStart_Click);
    this.cmdPause.Location = new System.Drawing.Point(232, 448);
    this.cmdPause.Name = "cmdPause";
    this.cmdPause.TabIndex = 2;
    this.cmdPause.Text = "Pause";
    this.cmdPause.Click += new System.EventHandler(this.cmdPause_Click);
    this.cmdResume.Location = new System.Drawing.Point(368, 448);
    this.cmdResume.Name = "cmdResume";
    this.cmdResume.TabIndex = 3;
    this.cmdResume.Text = "Resume";
    this.cmdResume.Click += new System.EventHandler(this.cmdResume_Click);
    this.cmdStop.Location = new System.Drawing.Point(480, 456);
    this.cmdStop.Name = "cmdStop";
    this.cmdStop.TabIndex = 4;
    this.cmdStop.Text = "Stop";
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(696, 501);
    this.Controls.Add(this.cmdStop);
    this.Controls.Add(this.cmdResume);
    this.Controls.Add(this.cmdPause);
    this.Controls.Add(this.cmdStart);
    this.Controls.Add(this.lstPrime);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);}
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
    t = new Thread(new ThreadStart(GenerateNum));
    t.Priority = System.Threading.ThreadPriority.BelowNormal;
    t.Start();
    }
    public void GenerateNum()
    {
    long[] NumArray = new long[256];
    for(int i = 1;i<255;i++)
    {
    UpdateValue pv = new UpdateValue(UpdateUI);
    string[] args = new string[] {i.ToString()};
    this.Invoke(pv,args);
    Thread.Sleep(200);

    }
    } private void cmdStart_Click(object sender, System.EventArgs e)
    {
    primeNumberThread = new Thread(new ThreadStart(GenerateNum));
    primeNumberThread.Name = "Prime Number Example";
    primeNumberThread.Priority = System.Threading.ThreadPriority.BelowNormal;
    cmdPause.Enabled = true;
    cmdStart.Enabled = false;
    primeNumberThread.Start();
    } private void cmdPause_Click(object sender, System.EventArgs e)
    {
    try
    {
    if(primeNumberThread.ThreadState == System.Threading.ThreadState.Running)
    {
    primeNumberThread.Suspend();
    cmdPause.Enabled = false;
    cmdResume.Enabled = true;
    }
    }
    catch(ThreadStateException ex)
    {
    MessageBox.Show(ex.ToString(),"Exception",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
    }
    } private void cmdResume_Click(object sender, System.EventArgs e)
    {
    if(primeNumberThread.ThreadState == System.Threading.ThreadState.Suspended || primeNumberThread.ThreadState == System.Threading.ThreadState.SuspendRequested)
    {
    try
    {
    primeNumberThread.Resume();
    cmdResume.Enabled = false;
    cmdPause.Enabled = true;
    }
    catch(ThreadStateException ex)
    {
    MessageBox.Show(ex.ToString(),"Exception",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
    }
    }
    } private void button1_Click_1(object sender, System.EventArgs e)
    {
    MessageBox.Show("我日","Exception",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
    } public void UpdateUI(string result)
    {
    lstPrime.Items.Add(result);
    }
    }
    }
      

  2.   

    晕  才来一位兄弟啊  我是刚学习多线程,但是我觉得不会出现这种情况 因为线程是在运行的 要挂起肯定是要判断 if(primeNumberThread.ThreadState == System.Threading.ThreadState.Running)
    但是为什么primeNumberThread的状态却又是WaitSleepJoin的呢??