回 帖 发 新 帖 刷新版面

主题:[讨论]我的Chess程序的出现的多线程的问题,请高手帮我看一下。万分感谢!

[code=c]
请填写代码
[/code]
case WM_LBUTTONDOWN:
          
            dx=LOWORD(lParam);
            dy=HIWORD(lParam);
            [color=FFFF00]/*if(!UpBDN&&!DownBDN&&DNOne)
            {
                ENDP=true;
                DNOne=false;
            }*/[/color]            
            if(!UpBDN&&!DownBDN)
            {
                Over=false;
                BPLANDOWN=true;
                while(!VR&&!VB&&BREGRETDOWN)
                {   
                    if (TurnRGo)
                    {                       
                           
                        if(ReturnCheck)
                        {
                            OutputPMove();
                            ReturnCheck=false;
                        }
                        
                               // Start the child process. 
                        [color=00FF00]CreateProcess( NULL, // No module name (use command line). 
                                   "JSearch.exe", // Command line. 
                                   NULL,             // Process handle not inheritable. 
                                   NULL,             // Thread handle not inheritable. 
                                   FALSE,            // Set handle inheritance to FALSE. 
                                   0,                // No creation flags. 
                                   NULL,             // Use parent's environment block. 
                                   NULL,             // Use parent's starting directory. 
                                   &si,              // Pointer to STARTUPINFO structure.
                                   &pi );             // Pointer to PROCESS_INFORMATION structure.                                                                            
                        
                        /*if(ENDP)
                        {
                            TerminateProcess(pi.hProcess,0);
                            ENDP=false;
                            UpBDN=true;
                             DownBDN=true;
                            break;
                        }
                        DNOne=true;*/
                        // Wait until child process exits.
                        WaitForSingleObject( pi.hProcess, INFINITE );

                        // Close process and thread handles. 
                        CloseHandle( pi.hProcess );
                        CloseHandle( pi.hThread );[/color]    
                                            
                        FIBManNMove=fopen("OUTBManNMOVE","rb");
                        fscanf(FIBManNMove,"CI=%d CJ=%d NI=%d NJ=%d",&BManMoveCI,&BManMoveCJ,&BManMoveNI,&BManMoveNJ);                                                                                
                          fclose(FIBManNMove);
                        
                        ScanUserTime();
                        RManUserTimeS+=INUserTime;
                        RManUT.Hour=int(RManUserTimeS/3600);
                        RManUT.Minute=int(RManUserTimeS/60)-60*RManUT.Hour;
                        RManUT.Second=RManUserTimeS-3600*RManUT.Hour-60*RManUT.Minute;
                        DelayTime=0;
                           if(RDown)
                               DNMoveRMan(BManMoveCI,11-BManMoveCJ,BManMoveNI,11-BManMoveNJ);
                           else if(BDown)
                               DNMoveRMan(BManMoveCI,BManMoveCJ,BManMoveNI,BManMoveNJ); 
                    
                        while(DelayTime<=685)
                        {
                            DelayTime++;
                            MyPaint(hdc);                            
                        }
                        DelayTime=0;
                        CONToChessBoard();
                        
                        CheckR();                    
                        BManMoveCI=0;
                        BManMoveCJ=0;
                        BManMoveNI=0;
                        BManMoveNJ=0;
                        FIBManNMove=fopen("OUTBManNMOVE","wb");
                        fclose(FIBManNMove);                        
                           TurnRGo=false;
                           TurnBGo=true;
    
                    }
                    if (TurnBGo)
                    {
                                                fclose(FOBManCMove);

                        if(ReturnCheck)
                        {
                            OutputPMove();
                            ReturnCheck=false;
                        }    
                         // Start the child process. 
                        CreateProcess( NULL, // No module name (use command line). 
                                   "JSearch.exe", // Command line. 
                                   NULL,             // Process handle not inheritable. 
                                   NULL,             // Thread handle not inheritable. 
                                   FALSE,            // Set handle inheritance to FALSE. 
                                   0,                // No creation flags. 
                                   NULL,             // Use parent's environment block. 
                                   NULL,             // Use parent's starting directory. 
                                   &si,              // Pointer to STARTUPINFO structure.
                                   &pi );             // Pointer to PROCESS_INFORMATION structure.
                                                                                     
                        /*if(ENDP)
                        {
                            TerminateProcess(pi.hProcess,0);
                            ENDP=false;
                            UpBDN=true;
                             DownBDN=true;
                            break;
                        } 
                        DNOne=true;    */
                        // Wait until child process exits.
                        WaitForSingleObject( pi.hProcess, INFINITE );

                        // Close process and thread handles. 
                        CloseHandle( pi.hProcess );
                        CloseHandle( pi.hThread );    
                        
                        FIBManNMove=fopen("OUTBManNMOVE","rb");
                        fscanf(FIBManNMove,"CI=%d CJ=%d NI=%d NJ=%d",&BManMoveCI,&BManMoveCJ,&BManMoveNI,&BManMoveNJ);                                                                                
                          fclose(FIBManNMove);

                        ScanUserTime();
                        BManUserTimeS+=INUserTime;
                        BManUT.Hour=int(BManUserTimeS/3600);
                        BManUT.Minute=int(BManUserTimeS/60)-60*BManUT.Hour;
                        BManUT.Second=BManUserTimeS-3600*BManUT.Hour-60*BManUT.Minute;
                        DelayTime=0;                 
                           if(RDown)
                               DNMoveBMan(BManMoveCI,BManMoveCJ,BManMoveNI,BManMoveNJ);
                           else if(BDown)
                            DNMoveBMan(BManMoveCI,11-BManMoveCJ,BManMoveNI,11-BManMoveNJ);    
                        
                        while(DelayTime<=685)
                        {    
                            DelayTime++;
                            MyPaint(hdc);                        
                        } 
                        DelayTime=0;
                        CONToChessBoard();
                        
                           CheckB();
                        BManMoveCI=0;
                        BManMoveCJ=0;
                        BManMoveNI=0;
                        BManMoveNJ=0;
                        FIBManNMove=fopen("OUTBManNMOVE","wb");
                        fclose(FIBManNMove);                        
                           TurnRGo=true;
                           TurnBGo=false;                                                                                        
                    }
                }
            }        
            break;
            我是想在界面点击鼠标左键后中断JSearch线程,但是主程序不能接收鼠标的消息,一点鼠标就死机。请高手帮我解决,我万分感谢,也会帮帮忙解决我能解决的问题的。我是新来的,请多照顾。

回复列表 (共5个回复)

沙发

代码没有仔细看, CreateProcess、TerminateProcess是进程操作函数。你创建一个运行的进程后,进程是活动的,WaitForSingleObject( pi.hProcess, INFINITE )将阻塞。通常你要结束一个进程是不宜采用这种方法的,你需要把创建部分和结束进程部分分开,逻辑上需要设计合理一些。而且,通常SDK编程,不宜把消息分支写得太长,可以用函数模块化,可以使用消息分流器(详见《windows核心编程》附录)。这样更易于修改和阅读。

板凳


谢谢费心的回答,你的意思是用WaitForSingleObject( pi.hProcess, INFINITE )主线程将阻塞,但我要等子线程运行完后,再执行主线程,还有其他的方法吗?

3 楼

首先,创建线程是用_beginthreadex或者CreateThread,CreateProcess是创建进程。其次,用_beginthreadex或者CreateThread成功后,将得到一个线程句柄,用SuspendThread和ResunmeThread可以控制该线程的挂起和执行状态。如果你要控制进程状态的话,你可以采用进程间通讯(IPC)的方法,对于棋类游戏,采用多线程控制很有必要。另外,TerminateProcess、TerminateThread之类的函数请尽量不要调用,而是让线程自然return 比较友好,因为这样可以正确释放我们之前的资源。你可以设定一个线程安全的全局变量控制线程退出。用waitformultiobject/msgwaitformultiobject控制线程的顺序安全退出。

4 楼

waitformultiobject/msgwaitformultiobject不知道怎么用,可能要买一本书看了。
进程间通讯(IPC)的方法。你能推荐一本书吗?
谢谢费心的解答。

5 楼

咱也学习了。

我来回复

您尚未登录,请登录后再回复。点此登录或注册