下面代码写在一个com组件里
while(1)
{
int j=select(0,&fset,0,0,0);
if(j)
{
DWORD dwThreadId; 
            HANDLE hThread; 
        hThread = CreateThread( 
        NULL,                        // default security attributes 
        0,                           // use default stack size  
        ThreadFunc,                  // thread function 
        &sck,                // argument to thread function 
        0,                           // use default creation flags 
        &dwThreadId);                // returns the thread identifier 
}
}
问题一 while循环使组件的调用者CPU占用居高不下,如何修改才能把CPU占用将下来。
问题二 当代码所在的函数(方法)被调用后,如何才能结束它?

解决方案 »

  1.   

    你需要在while中设一个判断条件退出
      

  2.   

    我来抢分:
    一、while循环中必须经常调用Sleep()函数,否则CPU占用率必为100%。
    二、你的意思是说if(j)中的代码被调用吧?那么简单return就可以退出线程了。
      

  3.   

    在 while 循环内部加一条 Sleep(0);
      

  4.   

    我也碰到了在线程里需要执行while循环,CPU居高不下,
    加了Sleep(0)后没有什么改善呀!还有什么好的解决方法呀?
      

  5.   

    while(1)
    {
    int j=select(0,&fset,0,0,0);
    if(j)
    {
    DWORD dwThreadId; 
                HANDLE hThread; 
            hThread = CreateThread( 
            NULL,                        // default security attributes 
            0,                           // use default stack size  
            ThreadFunc,                  // thread function 
            &sck,                // argument to thread function 
            0,                           // use default creation flags 
            &dwThreadId);                // returns the thread identifier 
    }
    }
    死循环,当然会死。
    建议楼主读读<windows网络编程>
      

  6.   

    最好使用WSAEventSelect IO模型,(WSAAsyncSelect需要窗口句柄),当然完成端口也行。
      

  7.   

    为何不把while循环写到ThreadFunc函数里面呢?试一下吧!应该可以的。
      

  8.   

    主界面上死循环当然要死 线程里sleep(10); 不是sleep(0);
      

  9.   

    同意 lovingyou() 
    建议你创建一个线程,在这个线程的回调函数里用while循环,这样肯定不会cpu占满了
      

  10.   

    实验发现,CPU居高不下并不是因为while循环,而是代码的其他部分出了问题。
    现在有一个问题:
    如果我在VB脚本里调用组件,因为代码的循环不会结束,所以我就无法执行下一条语句,也就无法设置条件让程序终止。(期望的效果是程序不停的运行,除非收到停止的指令)
    如果把这段代码放在一个新的线程里,主线程当然可以继续运行,并可能检测到停止的指令。但主线程运行结束呢?岂不是程序的目的达不到了?(VB脚本是顺序执行的呀,要么死在某一句上,要么到最后一行结束)
      

  11.   

    不需要窗口一定选WSAEvenSelect()+Overlapped+多线程
      一般的服务器足以,高性能的服务器还是考虑用IOCP
      

  12.   

    The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O.int select(
      int nfds,
      fd_set* readfds,
      fd_set* writefds,
      fd_set* exceptfds,
      const struct timeval* timeout
    );Parameters
    nfds 
    [in] Ignored. The nfds parameter is included only for compatibility with Berkeley sockets. 
    readfds 
    [in, out] Optional pointer to a set of sockets to be checked for readability. 
    writefds 
    [in, out] Optional pointer to a set of sockets to be checked for writability. 
    exceptfds 
    [in, out] Optional pointer to a set of sockets to be checked for errors. 
    timeout 
    [in] Maximum time for select to wait, provided in the form of a TIMEVAL structure. Set the timeout parameter to null for blocking operations. The parameter time-out controls how long the select can take to complete. If time-out is a null pointer, select will block indefinitely until at least one descriptor meets the specified criteria. Otherwise, time-out points to a TIMEVAL structure that specifies the maximum time that select should wait before returning. When select returns, the contents of the TIMEVAL structure are not altered. If TIMEVAL is initialized to {0, 0}, select will return immediately; this is used to poll the state of the selected sockets. If select returns immediately, then the select call is considered nonblocking and the standard assumptions for nonblocking calls apply. For example, the blocking hook will not be called, and Windows Sockets will not yield.dwTimeOut == 0 就是一直等待,直到有数据,且不占用cpu的。   BOOL WaitForData(DWORD dwTimeOut = 0) const
       {
      if ( m_hSocket == INVALID_SOCKET ) return FALSE;
          TIMEVAL tm = { (long)(dwTimeOut / 1000), (long)(dwTimeOut % 1000) };
          while( true ) {
             fd_set fdread;
             FD_ZERO(&fdread);
             FD_SET(m_hSocket, &fdread);
             int ret = ::select(0, &fdread, NULL, NULL, dwTimeOut == 0 ? NULL : &tm);
             switch( ret ) {
             case 0:
                return FALSE; // timeout
             case SOCKET_ERROR:
                if( ::WSAGetLastError() == WSAEINPROGRESS ) return FALSE; // busy
                // BUG: Hmm, this causes the code to continue and the next Read() to fail
                return TRUE;
             case 1:
                if( FD_ISSET(m_hSocket, &fdread) ) return TRUE;
                // FALL THROUGH...
             default:
                _ASSERTE(false);
             }
          }
       }