MFC "17-3" thread and thread synchronization

Source: Internet
Author: User

17.3 Small Knowledge Point 17.3.1 message pump

Write an application that responds to a menu command and draws thousands of ellipses.

1 voidCMFC Thread View::onstartdrawing (void)2 {3m_bquit=FALSE;4      for(intI=0; i<numellipses&&!m_bquit;i++)5     {6 drawrandomellipse ();7         if(!peekandpump ())8              Break;9     }Ten } One  A  - voidCMFC Thread View::onstopdrawing (void) - { them_bquit=TRUE; - } -  -  + BOOLCMFC thread View::P eekandpump (void) - { + msg msg; A      while(::P eekmessage (&msg,null,0,0, Pm_noremove)) { at         if(! AfxGetApp ()PumpMessage ()) { -::P Ostquitmessage (0); -             returnFALSE; -         } -     } -LONG 1idle=0; in      while(AfxGetApp ()->onidle (1idle++)); -     returnTRUE to}

The Peekandpump customizes another message loop in a message loop. It is called at the end of the loop for the for statement in Onstartdrawing. If::P eekmessage indicates that there is a message waiting in the queue, Peekandpump first calls CWinThread: Extract the message:P Umpmessage and dispatch the message. If PumpMessage returns 0, the last message that is fetched and dispatched is the Wm_quit message. This message requires special handling because only the wm_quit message is extracted with the "primary" message loop and the application ends. So if PumpMessage returns 0,peekandpump, another wm_quit message is sent to the queue, and if Peekandpump returns 0, another wm_quit message is sent to the queue, and if Peekandpump returns 0, The For Statement loop in Onstartdrawing will fail. If the Wm_quit message does not prompt for early exit, the peekandpump can mimic the idle mechanism of the main program by invoking the OnIdle function of the Application object before returning.

17.3.2 Performing Other processes

Win32 the execution process. The following statement executes C:\\windows\notepad.exe.

1 startupinfo si;2:: ZeroMemory (&si,sizeof(Startupinfo));3si.cb=sizeof(startupinfo);4 process_information Pi;5 6     if(:: CreateProcess (null,_t ("C:\\windows\\notepard"), Null,null,false,normal_priority_class,7null,null,&si,&pi)) {8 :: CloseHandle (Pi.hthread);9 :: CloseHandle (pi.hprocess);Ten}

:: CreateProcess is a general function that gets the name (and path) of the executable file and then loads and executes it if the drive and directory names in the executable file name are omitted, then the system is automatically in the Windows directory, the Windows system directory, Searches for the file in all directories under the current path and in other locations that are selected. The file name can also contain command-line arguments, such as:

"C:\\windows\\notepad C: \ \ windows\\ Desktop\\ideas.txt"

:: Creatprocess The key information for the process is populated in the process_information structure. Related information includes the process handle (hprocess) and the handle (hthread) of the main thread in the process. After the process starts, use:: CloseHandle to close these handles. If CreateProcess returns a value other than 0, it means that the process started successfully. Because Win32 is a startup, one execution, CreateProcess does not have to wait until the process is finished to return. If you want to start another process and pause the current process knowing that the process is started by the end of the process, you can call the process handle:: WaitForSingleObject

    startupinfo si;    :: ZeroMemory (&si,sizeof(startupinfo));    SI.CB=sizeof(startupinfo);    Process_information Pi;     if (:: CreateProcess (null,_t ("c:\\windows\\notepard"), Null,null,false,normal _priority_class,        null,null,&si,&pi)) {            :: CloseHandle (Pi.hthread);::            WaitForSingleObject (Pi.hprocess,infinite);            :: CloseHandle (pi.hprocess);    }

The process and thread have exit codes as well. If:: WaitForSingleObject does not return wait_failed, you can call:: GetExitCodeProcess gets the exit code for the process.

Sometimes you need to start a process and wait long enough to ensure that the process has started and that the user enters it accordingly. For example, if process a starts process B and process B creates a window, which is, if process a wants to send a message to the window, he will have to wait until CreateProcess returns, leaving process B enough time to create the window and start processing the message. This problem can be solved by the Win32::waitforinputidle function.

    startupinfo si;    :: ZeroMemory (&si,sizeof(startupinfo));    SI.CB=sizeof(startupinfo);    Process_information Pi;     if (:: CreateProcess (null,_t ("c:\\windows\\notepard"), Null,null,false,normal _priority_class,        null,null,&si,&pi)) {            :: CloseHandle (Pi.hthread);::            WaitForInputIdle (Pi.hprocess,infinite);            :: CloseHandle (pi.hprocess);    }

17.3.3 File Change Notification

:: WaitForSingleObject The handle parameter can be "File change notification handle". The Win32 API contains a function:: FindFirstChangeNotification, whenever a given directory or his subdirectory changes, such as when a file is renamed or deleted, or a new directory is created, the function can return a handle. With this handle, you can start a blocked thread.

If you want to improve the Wanderer application in Chapter 11, it is difficult to change the file system so that it is immediately reflected in the left or right pane. The most effective way to do this is to start a background thread and make it blocked on one or more file change notification handles. The following is the thread function used to monitor the thread of drive C::

UINT ThreadFunc (LPVOID pparam) {HWND hwnd= (HWND) pparam;//Window to notifyHANDLE Hchange =:: FindFirstChangeNotification (_t ("c:\\"), true,file_notify_change_file_name);//file_notify_change_dir_name);    if(hchange==Invalid_handle_value) {TRACE (_t ("error:findfirstchangenotification failed\n")); return(UINT)-1;}  while() {:: WaitForSingleObject (Hchange,infinite); ::P ostmessage (hwnd,wm_user_change_notify,0,2); :: Findnextchangenotification (Hchange);//Reset}:: Findclosechangenotification (Hchange); return 0;}

MFC "17-3" thread and thread synchronization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.