void CWinreplayDlg::OnButtonStartcapture()  //一个按钮“开始捕获”的消息响应函数
{
pcap_if_t *alldevs;
    pcap_if_t *d;
    int inum;
    int i=0;
    pcap_t *adhandle;
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_dumper_t *dumpfile;
    CString tempbuff;////////////////////////////////其他代码/////////////////////////////////* 打开适配器*/
    if ( (adhandle = pcap_open_live(d->name, // name of the device
                             65536,     // portion of the packet to capture. 
                                        // 65536 grants that the whole packet will be captured on all the MACs.
                             1,         // promiscuous mode
                             1000,      // read timeout
                             errbuf     // error buffer
                             ) ) == NULL)
    {
       ................        /* 释放设备列表 */
        pcap_freealldevs(alldevs);
        return ;
    }    /* 打开嗅探文件 */
    dumpfile = pcap_dump_open(adhandle, "c:\\capture.txt");
    
......................................其他代码
    
    /* 开始捕获 */
pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);//winpcap库中的函数,包含死循环,当点击“开始捕获”按钮后,其他的按钮都无法点击,程序就象死掉一样。 return ;
}要求:使用_beginthreadex(   )开启一个线程,使得用户可以点击对话框中的“停止捕获”按钮,当点击停止捕获按钮后,捕获结束,但是对话框不消失。请高手给我写一下具体的代码!!!!拜托了

解决方案 »

  1.   

    你需要在 你的循环中作一个判断:if(loop),当按下开始捕获时,令loop为TURE,当停止捕获时,为FALSE,这样就可以满足你的要求了
      

  2.   

    WORD threadNum = system_info. DwNumberOfProcessors*2+2;
    for(int i=0;I<threadNum;i++)
    {
        HANDLE hThread;
        DWORD dwthreadId;
        hThread = _beginthreadex(NULL,ServerWorkThrea,  (LPVOID)hCompletePort,0,&dwthreadId);
        CloseHandle(hThread); 
    }
    CloseHandle(hThread)在程序代码中的作用是在工作线程在结束后,能够自动销毁对象作用。