then how did you know when it is over? public void Thread_WorkCommand() { int iLength = 0; int iUserID = 0; while(true) { if(m_UserSate.GetBuffCount() != 0) { byte[] byBuff = m_UserSate.GetBuffer(ref iLength,ref iUserID); CommandCase(byBuff,iLength,iUserID); m_UserSate.DelBuff(); } if (notover) //<-----change it here System.Threading.Thread.Sleep(500); //sleep half seconds else break; } }
public void Thread_WorkCommand()
{
int iLength = 0;
int iUserID = 0;
while(true)
{
if(m_UserSate.GetBuffCount() != 0)
{
byte[] byBuff = m_UserSate.GetBuffer(ref iLength,ref iUserID);
CommandCase(byBuff,iLength,iUserID);
m_UserSate.DelBuff();
} if (notover) //<-----change it here
System.Threading.Thread.Sleep(500); //sleep half seconds
else
break;
}
}
两个线程在不同的类中,我想使用ManualResetEvent来同步两个线程,但是ManualResetEvent这个东西我还不大会使用,似乎在两个类中不能互相控制,除非ManualResetEvent是全局变量。
请教大家了。
这两个线程都是异步的,一个不断的往公共类中添加数据,一个线程在不断循环访问这个公共类,看看之中有没有要处理的数据。
我写好以后发现,CPU的占用率一直在100%,即使没有数据,也在空转。
我不想让它有这么大的资源占用,如果没有数据就不用空转,将线程挂起,不知道我说明白了没有?
http://www.codeproject.com/csharp/eventsthreadsync.asp?df=100&forumid=3543&exp=0&select=143730