社区
C#
帖子详情
多线程问题,运行中的线程的状态为什么是WaitSleepJoin状态,迷茫中!
pq16344
2006-04-17 06:15:24
点start启动程序,但是点pause不会挂起,然后查看primeNumberThread的状态发现是WaitSleepJoin
为什么运行中的线程会是"等待睡眠连接"啊
...全文
98
2
打赏
收藏
多线程问题,运行中的线程的状态为什么是WaitSleepJoin状态,迷茫中!
点start启动程序,但是点pause不会挂起,然后查看primeNumberThread的状态发现是WaitSleepJoin 为什么运行中的线程会是"等待睡眠连接"啊
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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);
}
}
}
多
线程
学习笔记——判断
线程
状态
C#
多
线程
状态
判断http://blog.csdn.net/deandingding/article/details/39762489 C#
多
线程
有很多值得学习的地方,这里我们主要介绍C# ThreadState属性,包括介绍主
线程
Main()函数和
线程
的优先级等方面。 主
线程
Main...
C#
线程
状态
简析
在C# 语言世界
中
,当我们创建一个新的子
线程
,该
线程
状态
为unstarted, 子
线程
开始之后,子
线程
状态
为Running,IsAlive 为true;
线程
响应 Thread.Start 并开始
运行
。 Running
线程
调用 Thread.
Sleep
。 ...
C#语言
中
的
多
线程
本文建议考虑在C#
中
使用
多
线程
。它讨论了Thread类、创建
线程
、ThreadStart委托、带参数的
线程
、
线程
同步、
线程
监视器、AutoResetEvent 类以及为什么需要它、互斥体和信号量。
C#--检索
线程
状态
Thread对象的生存期特征由一组
状态
描述。Thread对象的ThreadState属性将返回下列10个数值
中
的一个: Unstarted
线程
尚未开始 Running
线程
正则执行 Background
线程
正在后台执行
Wait
Sleep
Join
线程
由于...
C#
多
线程
Thread类各
状态
与函数之间的关系分析测试
C#的
多
线程
有多个
状态
和多个操作函数,本程序主要目的是为了测试C#
中
线程
的主要
状态
和操作函数调用关联。 基本概念 Thread类的
状态
使用枚举型 ThreadState 进行表示,其
中
有多种
状态
,大部分都是不用的,本文...
C#
110,534
社区成员
642,575
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章