多线程问题,运行中的线程的状态为什么是WaitSleepJoin状态,迷茫中!

pq16344 2006-04-17 06:15:24
点start启动程序,但是点pause不会挂起,然后查看primeNumberThread的状态发现是WaitSleepJoin
为什么运行中的线程会是"等待睡眠连接"啊
...全文
98 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pq16344 2006-04-17
  • 打赏
  • 举报
回复
麻烦大家帮忙看看程序 ,刚接触多线程
pq16344 2006-04-17
  • 打赏
  • 举报
回复
代码如下
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);
}
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧