| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5210 人关注过本帖
标题:[求助]线程状态waitsleepjoin怎么办?
只看楼主 加入收藏
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
 问题点数:0 回复次数:16 
[求助]线程状态waitsleepjoin怎么办?
using System;
using System.Threading;

class ApartmentTest
{
static void Main()
{
Thread newThread =
new Thread(new ThreadStart(ThreadMethod));
newThread.SetApartmentState(ApartmentState.MTA);

// The following line is ignored since
// ApartmentState can only be set once.
//newThread.SetApartmentState(ApartmentState.STA);

Console.WriteLine("\nThreadState: {0}, ApartmentState: {1}",
newThread.ThreadState, newThread.ApartmentState);

newThread.Start();
Console.WriteLine("\nThreadState: {0}, ApartmentState: {1}",
newThread.ThreadState, newThread.ApartmentState);
// Wait for newThread to start and go to sleep.
Thread.Sleep(300);
try
{
// This causes an exception since newThread is sleeping.
newThread.SetApartmentState(ApartmentState.STA);
}
catch (ThreadStateException stateException)
{
Console.WriteLine("\n{0} caught:\n" +
"Thread is not in the Unstarted or Running state.",
stateException.GetType().Name);
Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
newThread.ThreadState, newThread.GetApartmentState());
}


}

static void ThreadMethod()
{
Thread.Sleep(1000);
}
}



搜索更多相关主题的帖子: 线程 状态 newThread using The 
2006-08-31 09:49
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
输出:hreadState: Unstarted, ApartmentState: MTA

ThreadState: Running, ApartmentState: MTA

ThreadStateException caught:
Thread is not in the Unstarted or Running state.
ThreadState: WaitSleepJoin, ApartmentState: MTA

问题: 为什么最后的时候newthread的状态为 WaitSleepJoin?


2006-08-31 09:55
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
如果 我使用

newthread.resume()
程序提示:“线程不是用户挂起的;无法使其继续。”

但是我新创建另外一个线程,也是如此提示。


2006-08-31 09:57
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
是不是WaitSleepJoin以后就不能再使用该线程了啊?

2006-08-31 10:16
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
WaitSleepJoin
應該是必須等到指定時間過後吧,不過可以abort supend 好像

resume之前必須先supend 否則會出現上面的錯誤

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-31 10:19
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
try
{
// This causes an exception since newThread is sleeping.
newThread.SetApartmentState(ApartmentState.STA);
********************************************
是不是因为这个语句设置本该在线程启动之前就应该设置好的属性所以导致异常。
但是为什么会使该线程状态变为waitsleepjoin()呢?
*******************************************
}
catch (ThreadStateException stateException)
{
Console.WriteLine("\n{0} caught:\n" +
"Thread is not in the Unstarted or Running state.",
stateException.GetType().Name);
Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
newThread.ThreadState, newThread.GetApartmentState());
}



2006-08-31 10:41
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
catch (ThreadStateException stateException)
{
Console.WriteLine("\n{0} caught:\n" +
"Thread is not in the Unstarted or Running state.",
stateException.GetType().Name);
***************************
newThread.Suspend();
newThread.Resume();
加上这两句就出错。
“线程目前未运行;无法使其挂起。”
***************************
Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
newThread.ThreadState, newThread.GetApartmentState());
}

2006-08-31 10:43
CrazyWeed0907
Rank: 2
等 级:新手上路
威 望:5
帖 子:1385
专家分:0
注 册:2006-5-30
收藏
得分:0 

因为thread.sleep


“十步杀一人,千里不留行。事了拂衣去,深藏身与名。”
2006-08-31 11:25
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
catch出錯後
newThread是被強制關了吧?

你sleep了 所以是waitsleepjoin

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-31 11:26
CrazyWeed0907
Rank: 2
等 级:新手上路
威 望:5
帖 子:1385
专家分:0
注 册:2006-5-30
收藏
得分:0 
catch (ThreadStateException stateException)
{
Console.WriteLine("\n{0} caught:\n" +
"Thread is not in the Unstarted or Running state.",
stateException.GetType().Name);
***************************
newTread.join(300) ;
newThread.Suspend();
newThread.Resume();
加上这两句就出错。
“线程目前未运行;无法使其挂起。”
***************************
Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
newThread.ThreadState, newThread.GetApartmentState());
}

“十步杀一人,千里不留行。事了拂衣去,深藏身与名。”
2006-08-31 11:35
快速回复:[求助]线程状态waitsleepjoin怎么办?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051763 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved