Home > Software engineering >  Small white, small white, small white, please! O! O! The great spirit about VS2013 multithreaded! O!
Small white, small white, small white, please! O! O! The great spirit about VS2013 multithreaded! O!

Time:10-04

Great god help me in baidu, anyway find changed, MFC multithreading how also not successful

Routine 10 MultiThread10

Establish A dialog based engineering MultiThread10, add A button in the dialog box IDD_MULTITHREAD10_DIALOG and three edit box control, button ID for IDC_START, titled "at the same time write 'A', 'B', 'C'"; Three edit box ID IDC_A respectively, IDC_B and IDC_C, attributes are selected to Read - only;
Two threads in MultiThread10Dlg. H file statement function:
 UINT WriteA (LPVOID pParam); 
UINT WriteB (LPVOID pParam);
UINT WriteC (LPVOID pParam);

Using ClassWizard to IDC_A respectively, IDC_B and IDC_C added CEdit class variables m_ctrlA, m_ctrlB and m_ctrlC;
In MultiThread10Dlg. CPP file add the following content:
In order to use synchronous class file correctly, at the beginning of the file to add:
 
# include "afxmt. H"

Define the semaphore object and an array of characters, in order to be able to use in different threads, defined as global variables:
 CSemaphore semaphoreWrite (2, 2);//resources access thread 2, most current accessible threads 2 
Char g_Array [10].

Add three thread function:

 UINT WriteA (LPVOID pParam) 
{
PParam CEdit * pEdit=(CEdit *);
PEdit - & gt; SetWindowText (" ");
WaitForSingleObject (semaphoreWrite m_hObject, INFINITE);
Cstrings STR.
for(int i=0; i<10; I++)
{
PEdit - & gt; GetWindowText (STR);
G_Array [I]='A';
STR=STR + g_Array [I];
PEdit - & gt; SetWindowText (STR);
Sleep (1000);
}
ReleaseSemaphore (semaphoreWrite m_hObject, 1, NULL);
return 0;

}
UINT WriteB (LPVOID pParam)
{
PParam CEdit * pEdit=(CEdit *);
PEdit - & gt; SetWindowText (" ");
WaitForSingleObject (semaphoreWrite m_hObject, INFINITE);
Cstrings STR.
for(int i=0; i<10; I++)
{

PEdit - & gt; GetWindowText (STR);
G_Array [I]=' ' ' 'B;
STR=STR + g_Array [I];
PEdit - & gt; SetWindowText (STR);
Sleep (1000);
}
ReleaseSemaphore (semaphoreWrite m_hObject, 1, NULL);
return 0;

}
UINT WriteC (LPVOID pParam)
{
PParam CEdit * pEdit=(CEdit *);
PEdit - & gt; SetWindowText (" ");
WaitForSingleObject (semaphoreWrite m_hObject, INFINITE);
for(int i=0; i<10; I++)
{
G_Array [I]='C';
PEdit - & gt; SetWindowText (g_Array);
Sleep (1000);
}
ReleaseSemaphore (semaphoreWrite m_hObject, 1, NULL);
return 0;

}

No longer say more, the three threads function under the semaphore object has the state of the signal, thread execution to the WaitForSingleObject statements continue to execute, while the number of threads available minus 1; If thread execution to the WaitForSingleObject statement when no signal semaphore object, thread right here waiting, until the signal semaphore object thread to execute,
Double click on the button IDC_START, add the response function:
 void CMultiThread10Dlg: : OnStart () 
{
CWinThread * pWriteA=AfxBeginThread (WriteA,
& M_ctrlA,
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);
PWriteA - & gt; ResumeThread ();

CWinThread * pWriteB=AfxBeginThread (WriteB,
& M_ctrlB,
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);
PWriteB - & gt; ResumeThread ();

CWinThread * pWriteC=AfxBeginThread (WriteC,
& M_ctrlC,
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);
PWriteC - & gt; ResumeThread ();


}


Great god help for detailed change bai!

I was such a




CodePudding user response:

Similar questions you asked?

CodePudding user response:

The first time to post,

CodePudding user response:

"Windows core programming"

CodePudding user response:

In the thread function ~ don't direct operation on the UI controls

CodePudding user response:

PostMessage in your thread to the main thread to update the UI controls

CodePudding user response:

Multiple Threads in the User Interface of http://msdn.microsoft.com/zh-cn/library/ms810439.aspx

CodePudding user response:

Operating the UI best response function to the news
  • Related