Welcome: Hunan Intelligent Applications Tecgnology CO.,ltd.-HNIAT.com
Language: Chinese ∷  English

Basic knowledge

MFC loop interface suspended solution (MFC button terminates the loop)

There are two ways:

1. Single-threaded solution:

OnBtn1 ()
{
        while (m_bLoop)
       {
                your code ....

              // dispatch the message
               MSG msg;
               if (PeekMessage (& msg, (HWND) NULL, 0, 0, PM_REMOVE))
               {
                        :: SendMessage (msg.hwnd, msg.message, msg.wParam, msg.lParam);
                }
       }
}

OnBtn2 ()
{
       m_bLoop = FALSE;
}

2. Multi-threading (reference: http://www.vckbase.com/document/viewdoc/?id=1704)

a. Define a function outside the class in xxxDlg.h (that is, outside the braces) void ThreadFunc (); (external function)

b. In the xxxDlg class, define a protected variable inside the CMultiThread1Dlg class:

HANDLE hThread; DWORD ThreadID;

     Represents the handle and ID of the thread.

c. Define the global variable volatileBOOL m_Flag in xxxDlg.cpp;

d. Write ThreadFunc function in xxxDlg.cpp

    void ThreadFunc ()

    {

           while (m_Flag)

           {

                    // your code here

            }

           return; // Thread suicide

    }

e. Button A represents the start of the cycle

 

void CxxxDlg :: OnStart ()

 

{

         hThread = CreateThread (

               NULL,

0, (LPTHREAD_START_ROUTINE) ThreadFunc, NULL, 0,

& ThreadID

);

}

f. Button B represents the end of the loop

 

void CxxxDlg :: OnStop () {m_bRun = FALSE;}

 

Other solutions:

Method <1>:

Write the loop part of the start button as a child thread separately, and create a child thread in the button's processing function, and the main thread will not block the message because of the loop.

# 7 楼 Reply to: 2007-05-09 18:15:50
It ’s ok to take a look at the thread. It is nothing more than putting the loop in a separate child thread. You are running in the main thread like that, which causes the interface to die. The main thread is blocked and busy processing your loop. So I do n’t care about responding to your other operations.

# 8 楼 Reply to: 2007-05-09 18:24:12
In the do while loop, add this sentence sleep (10).

The endless loop is put in the thread, so that the UI thread will not block, and the interface will not be faked.
In the endless loop, you can continuously send messages to the UI thread such as PostMessage to update the interface, such as a progress bar.


  
Winter and spring come back at 13:48 on the 6th
// You can put the following code in a loop to process the message, but it is better to use threads:

while (PeekMessage (& msg, NULL, NULL, NULL, PM_REMOVE))
{
    TranslateMessage (& msg);
    DispatchMessage (& msg);
}
SLEEP is the suspension of your current main thread, which will suspend the message response, so if a dead state occurs, you can throw another thread and sleep in that thread, so that the main thread will not be suspended


Reprinted at: https://www.cnblogs.com/MMLoveMeMM/articles/3089947.html

CONTACT US

Contact: Manager Xu

Phone: 13907330718

Tel: 0731-22222718

Email: hniatcom@163.com

Add: Room 603, 6th Floor, Shifting Room, No. 2, Orbit Zhigu, No. 79 Liancheng Road, Shifeng District, Zhuzhou City, Hunan Province

Scan the qr codeClose
the qr code