多线程问题,运行中的线程的状态为什么是WaitSleepJoin状态,迷茫中! 点start启动程序,但是点pause不会挂起,然后查看primeNumberThread的状态发现是WaitSleepJoin为什么运行中的线程会是"等待睡眠连接"啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 代码如下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); } }} 晕 才来一位兄弟啊 我是刚学习多线程,但是我觉得不会出现这种情况 因为线程是在运行的 要挂起肯定是要判断 if(primeNumberThread.ThreadState == System.Threading.ThreadState.Running)但是为什么primeNumberThread的状态却又是WaitSleepJoin的呢?? C# winform TextBox无法响应回车键 AE+C# intersect 分析,老是弹出错误 C#/Java读写Flash源文件 软件打包生成后与数据库连接的问题!! 就是连不上 VS2005中的PInvokeStackImbalance C#Winform编程DataGrid的鼠标双击的问题!在线等急呀 iis日志 百度帖吧现在有刷新漏洞,可以小利用下。 关于Visual C#中的combobox控件 如何获取某一个月的第一天或者第一天是星期几 提问:关于try finally
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);
}
}
}
但是为什么primeNumberThread的状态却又是WaitSleepJoin的呢??