51

Re: Operation with flows (threads) in MFC

I read chapters 6 - Jeffrey Richter's 11 books "WINDOWS. Creation of effective WIN32-appendices taking into account specificity of 64-digit Windows version": http://wm-help.net/books-online/book/59464.html And I had two questions: 1) When we cause function _beginthreadex after end of operation of a flow (as a result of usage of functions WaitForSingleObject or WaitForMultipleObjects) whether is mandatory it it is necessary to close a flow descriptor hThread (which has been created by function _beginthreadex) by means of function CloseHandle (hThread);? 2) whether In stream function is mandatory to use function _endthreadex for end of operation of a flow or it is possible to complete this flow with the help return in stream function? As I understood, for start of operation of a flow is better to use function _beginthreadex, than functions CreateThread or AfxBeginThread.

52

Re: Operation with flows (threads) in MFC

Hello, AlexGin, you wrote: RF>> That is if I function which produces calculations, mark as static it can be made stream? And whether there are in this case any singularities on operation with such function in comparison with global stream functions? AG> AG> to mark as "static" - probably causes a compilation error since there should not be a seamless access to  a class inside. And what, unless in any class its static function cannot address to nonstatic members (to functions and the data) this class? All difference only that nonstatic functions and the data form for each object of a class, and static functions and the class data exist in the only thing  - they concern not objects of a class, and to the class.

53

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> 1) When we cause function _beginthreadex after end of operation of a flow (as a result of usage of functions WaitForSingleObject or WaitForMultipleObjects) whether is mandatory it it is necessary to close a flow descriptor hThread (which has been created by function _beginthreadex) by means of function CloseHandle (hThread);? If the flow is created with the help _beginthread and the output from a flow is carried out by means of function invocation _endthread it is not necessary (she closes ). If the flow forms with usage AfxBeginThread, the same is not necessary. There inside the flow forms not directly, and there is a copy of class CWinThread, in  which is caused CloseHandle. In all remaining cases it is necessary to cause CloseHandle, differently there is a resource leak. RF> 2) whether In stream function is mandatory to use function _endthreadex for end of operation of a flow or it is possible to complete this flow with the help return in stream function? ExitThread is the preferred method of exiting a thread in a C code. However, in a C ++ code, the thread is exited before any destructors can be called or any other automatic cleanup can be performed. Therefore, in a C ++ code, you should return from your thread function. Clearly about what speech? Short - in programs on with ++ it is necessary to use return. RF> As I understood, for start of operation of a flow is better to use function _beginthreadex, than functions CreateThread or AfxBeginThread. _beginthread or _beginthreadex it is necessary to use if in a flow any functions CRT are caused. And generally Richter urgently recommends to use _beginthreadex. That in general it is logical. As a whole, what not to hammer in a head the superfluous information and to avoid all possible problems - correctly to use _beginthreadex for creation of a flow and return 0 for an output from it. With subsequent CloseHandle

54

Re: Operation with flows (threads) in MFC

Hello, Evgeniy Skvortsov, you wrote: ES> And generally Richter urgently recommends to use _beginthreadex. That in general it is logical. ES> as a whole what not to hammer in a head the superfluous information and to avoid all possible problems - correctly to use _beginthreadex for creation of a flow and return 0 for an output from it. With subsequent CloseHandle it is clear, understood. Thanks!

55

Re: Operation with flows (threads) in MFC

There is at me dialogue IDD_CALCMANDIALOG to which there corresponds class CCalcManDialog. On this dialogue buttons "Stop" are allocated, to "Continue", "Output" and component IDC_STATIC1 to which there corresponds a variable m_IDC_STATIC1 type CStatic. At discovery of this dialogue there are some actions which I placed in stream functions. The first variant - here in a flow are deduced in a console window of number from 1 to 20000: typedef struct {volatile int flag;} MyStruct; class CCalcManDialog: public CDialogEx {//the data and methods of class CCalcManDialog; MyStruct myStruct; unsigned threadID; HANDLE hThread;//methods and the data of class CCalcManDialog;} ; unsigned int __ stdcall MyThreadFunc (void* pParam) {int i; MyStruct *pMyStruct = (MyStruct *) pParam; volatile int *pflag = and (pMyStruct-> flag); for (i=0; i <20000; i ++) {printf ("i = %d\n", i); if (*pflag == 0) {printf ("The thread was terminated.\n");//the flow has been forcedly completed return 0;} if (*pflag == 2)//the flow has been paused {while ((*pflag! =0) && (*pflag! =1)) Sleep (1000); if (*pflag == 0) {printf ("The thread was terminated.\n");//the flow has been forcedly completed (after the suspension) return 0;}}} printf ("The thread was finished successfully.\n");//the flow was completed safely (naturally) return 0;} BOOL CCalcManDialog:: OnInitDialog () {CDialog:: OnInitDIalog (); myStruct.flag = 1; hThread = (HANDLE) _beginthreadex (NULL,0,&MyThreadFunc,&myStruct,0,&threadID); return TRUE;} void CCalcManDialog:: OnBnClickedOk ()//if the key to "Quit" {myStruct.flag = 0 has been pushed; WaitForSingleObject (hThread, INFINITE); CloseHandle (hThread); CDialogEx:: OnOk ();} In this variant all works normally - at dialogue discovery in a dialog box integer values (*pflag == 1) are deduced, at pushing button "Stop" (*pflag == 2) the flow pauses the operation and waits or the renewal (button "Continue", *pflag == 1), or the forced end (button "Quit", *pflag == 0). If the flow works and before its end there was a pushed button to "Quit" (*pflag == 0) the flow is forcedly completed before the termination of operation. If any of these buttons it has not been pushed, the flow is fulfilled before the end. The second variant - here in a flow is deduced in a GUI-dialogue window, in its component IDC_STATIC1 (to which there corresponds a variable m_IDC_STATIC1) in a cycle at first an inscription "Calculation is fulfilled", then in a second an inscription "Calculation is fulfilled.", then in a second an inscription "Calculation is fulfilled.", after that in a second an inscription "Calculation is fulfilled..." Further in a second an inscription "Calculation is fulfilled" etc.: class CCalcManDialog; typedef struct {CCalcManDialog *pCalManDialog; volatile int flag2;} MyStruct2; class CCalcManDialog: public CDialogEx {//the data and methods of class CCalcManDialog; MyStruct2 myStruct2; unsigned threadID2; HANDLE hThread2; void setTextIntoSTatic (char *s); CStatic m_IDC_STATIC1;//the data and methods of class CCalcManDialog;}; unsigned int __ stdcall MyThreadFunc2 (void* pParam) {int i; char s [50]; bool localflag = true; MyStruct2 *pMyStruct2 = (MyStruct2 *) pParam; CCalcManDialog *pCalcManDialog = pMyStruct-> pCalcManDialog; volatile int *pflag2 = and (pmyStruct-> flag2); while (localflag == true) {sprintf (s, "Calculation is fulfilled"); pCalcManDialog-> setTextIntoStatic (s); Sleep (1000); sprintf (s, "Calculation is fulfilled."); Sleep (1000); sprintf (s, "Calculation is fulfilled."); Sleep (1000); sprintf (s, "calculation is fulfilled..." ); Sleep (1000); if (*pflag2 == 0) {sprintf (s, "Calculation is fulfilled"); pCalcManDialog-> setTextIntoStatic (s); localflag = false; break;} if (*pflag2 == 2) {while ((*pflag2! =0) && (*pflag2! =1)) Sleep (1000); if (*pflag2 == 0) {sprintf (s, "Calculation is fulfilled"); pCalcManDialog-> setTextIntoStatic (s); localflag = false; break;}}} return 0;} void CCalcManDialog:: setTextIntoStatic (char *s) {m_IDC_STATIC1.SetWindowsTextA (s);} BOOL CCalcManDialog:: OnInitDialog () {CDialog:: OnInitDialog (); myStruct2.flag2 = 1; myStruct2.pCalcManDialog = this; hThread2 = (HANDLE) _beginthreadex (NULL,0,&MyThreadFunc2,&myStruct2,0,&threadID2); return TRUE;} void CCalcManDialog:: OnBnClickedOk () {myStruct2.flag2 = 0; WaitForSingleObject (hThread2, INFINITE); CloseHandle (hThread); CDialogEx:: OnOk ();} In this case at me the flow deducing creeping line is launched (inscriptions "Calculation is fulfilled", "Calculation is fulfilled." "Calculation is fulfilled.", "calculation is fulfilled..." ), but at pushing button "Quit" as CCalcManDialog:: OnBnClickedOk at line WaitForSingleObject (hThread2, INFINITE); at me the program hangs up (waits infinitely long though I and delivered a flag myStruct2.flag2 equal 0). If in this function to replace WaitForSingleObject (hThread2, INFINITE); on WaitForSingleObject (hThread2,2000); that the program waits two seconds, passes then on CloseHandle (hThread); and further - that is the flow is completed. But after an output from function CCalcManDialog:: OnBnClickedOk at me the debugger fails in function MyThreadFunc2 where it fights in one of places where I check value flag2 (that is myStruct2.flag2) on 0. How to rectify this error? How to make so that and by operation with GUI-dialogue (its components) my stream functions normally worked, that is that there was a normal end of their operation on a flag? Perhaps, somehow in another way it is necessary to cause function _beginthreadex or instead of it it is necessary to use function AfxBeginThread or class CWinThread?

56

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> How to rectify this error? How to make so that and by operation with GUI-dialogue (its components) my stream functions normally worked, that is that there was a normal end of their operation on a flag? I so understood you use a flow for a creeping line output? It is search. Generally to climb in window affairs from other flow is there is no buzzing and is fraught with unpredictable consequences and in particular . In your case the creeping line is better for implementing through the timer. That is you launch timer SetTimer (1000) and you process message WM_TIMER. In the output agent already you deduce the text as it is necessary. The system itself will cause the handler through the specified interval. When it is necessary to stop a timer call - you stop it by means of KillTimer. RF> Perhaps, somehow in another way it is necessary to cause function _beginthreadex or instead of it it is necessary to use function AfxBeginThread or class CWinThread? It here there is nothing.

57

Re: Operation with flows (threads) in MFC

Hello, Evgeniy Skvortsov, you wrote: ES> In your case the creeping line is better for implementing through the timer. That is you launch timer SetTimer (1000) and you process message WM_TIMER. In the output agent already you deduce the text as it is necessary. The system itself will cause the handler through the specified interval. When it is necessary to stop a timer call - you stop it by means of KillTimer. I see. To launch the timer, it is necessary to use function SetTimer and to ungear the timer, it is necessary to use function KillTimer: https://msdn.microsoft.com/ru-ru/library/49313fdf.aspx https://msdn.microsoft.com/ru-ru/library/sfs37s7c.aspx http://www.firststeps.ru/mfc/steps/r.php?32 It works for me. But how correctly to arrive with the timer if launched it once, then the user pushed button "Pause" (the timer should stop the operation), then the user pushes button "Continue" (the timer should earn anew), and then the user pushes button "Quit" (the timer should stop the operation)? How it correctly to organize?

58

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> But how correctly to arrive with the timer if launched it once, then the user pushed button "Pause" (the timer should stop the operation), then the user pushes button "Continue" (the timer should earn anew), and then the user pushes button "Quit" (the timer should stop the operation)? How it correctly to organize? By button "Pause" - KillTimer, by button "Continue" - SetTimer, by button "Quit" - KillTimer

59

Re: Operation with flows (threads) in MFC

Hello, Evgeniy Skvortsov, you wrote: ES> Hello, RussianFellow, you wrote: RF>> But how correctly to arrive with the timer if launched it once, then the user pushed button "Pause" (the timer should stop the operation), then the user pushes button "Continue" (the timer should earn anew), and then the user pushes button "Quit" (the timer should stop the operation)? How it correctly to organize? ES> by button "Pause" - KillTimer, by button "Continue" - SetTimer, by button "Quit" - KillTimer it is clear, thanks.