我编写了一个程序,循环执行一段操作并显示进度条,此时程序处于等待状态,我想能够实现随时取消此操作,该处是否用到多线程?能否举例并附段代码?

解决方案 »

  1.   

    不一定用多线程的,加上一段代码就行了
    while (1)
    {
    if (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
    {
    if(message.message==xxx消息)
    {
    ...
    }
    ::TranslateMessage(&message);
    ::DispatchMessage(&message);
    }
    ...
    }
      

  2.   

    要用线程。AfxBeginThread((AFX_THREADPROC)CAutoSelThread,this);
      

  3.   

    HWND hwnd; 
    BOOL fDone; 
    MSG msg; 
     
    // Begin the operation and continue until it is complete 
    // or until the user clicks the mouse or presses a key. 
     
    fDone = FALSE; 
    while (!fDone) 

        fDone = DoLengthyOperation(); // application-defined function 
     
        // Remove any messages that may be in the queue. If the 
        // queue contains any mouse or keyboard 
        // messages, end the operation. 
     
        while (PeekMessage(&msg, hwnd,  0, 0, PM_REMOVE)) 
        { 
            switch(msg.message) 
            { 
                case WM_LBUTTONDOWN: 
                case WM_RBUTTONDOWN: 
                case WM_KEYDOWN: 
                    // 
                    // Perform any required cleanup. 
                    // 
                    fDone = TRUE; 
            } 
        }