何不 找个例子看看?就MSDN的那个了---worker thread.

解决方案 »

  1.   

    UINT SendThread(LPVOID pParam )    //定义线程函数
    {
    HANDLE hEvent=(HANDLE)pParam;
        while(TRUE!=0)
        {
    if (WaitForSingleObject(hEvent,nTimeout)==WAIT_OBJECT_0)
    return 0;    ::MessageBox(0, TEXT("Hi, here is your very first MFC based NT-service"), TEXT("MFC SampleService"), MB_OK);
        ::Sleep( 10000 );
            }
        return 0;
    }void CServiceApp :: Run( DWORD, LPTSTR *)//在service进程中建立新的线程
    {        
        ReportStatus(SERVICE_START_PENDING);
        m_hStop = ::CreateEvent(0, TRUE, FALSE, 0);//建立事件
            if( m_hStop )
            ::CloseHandle(m_hStop);
        ReportStatus(SERVICE_RUNNING);
            CWinThread* g_pThread = AfxBeginThread(SendThread,m_hStop);
    }void CServiceApp :: Stop()
    {
        ReportStatus(SERVICE_STOP_PENDING, 11000);
        if( m_hStop )
            ::SetEvent(m_hStop);
    }