1.事件:
(在信息交换函数中将控件的值与控件id进行绑定,这样我们就可以更新或者获取控件的值)
1 void CMy0722ThreadTalkingDlg::DoDataExchange(CDataExchange* pDX) 2 { 3 CDialogEx::DoDataExchange(pDX); 4 DDX_Radio(pDX, IDC_RADIO1, m_radio); 5 DDX_Control(pDX, IDC_COMBO1, m_test); 6 }
1 void CMy0722ThreadTalkingDlg::OnBnClickedButton1() 2 { 3 4 //创建线程 5 if(!m_hThread) 6 { 7 m_hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);//在线程创建之前创建事件 8 //安全属性,(重置事件)人工为true,信号(false对应为无信号),事件名字(跨进程时可以使用)); 9 m_hThread = (HANDLE)_beginthreadex(NULL,0,&CMy0722ThreadTalkingDlg::ThreadProc,this,0,NULL); 10 11 } 12 13 } 14 15 unsigned _stdcall CMy0722ThreadTalkingDlg::ThreadProc( void * lpvoid) 16 { 17 CMy0722ThreadTalkingDlg *pthis = ( CMy0722ThreadTalkingDlg *)lpvoid; 18 CString str; 19 while(pthis->m_bFlagQuit) 20 { 21 //等事件 22 if(WAIT_TIMEOUT == WaitForSingleObject(pthis->m_hEvent,100)) 23 { 24 continue; 25 } 26 int nSum = 0; 27 for(int i = 1;i <= pthis->m_nCalc;i++) 28 { 29 nSum+= i; 30 } 31 32 //第一种方式:事件 35 str.Format(_T("%d"),nSum); 36 pthis->GetDlgItem(IDC_EDIT1)->SetWindowText(str); 38 39 } 40 return 0; 41 } 42 53 void CMy0722ThreadTalkingDlg::OnBnClickedButton2() 54 { 55 56 //计算 57 // m_radio = 1; 58 //UpdateData( FALSE); //初始化对话框数据 59 //获得要计算的值 60 UpdateData(TRUE); //从对话框上获得数据 61 switch (m_radio)//此处用了组控件,但m_radio与tab键编号多少无关,仅是一句tab键的顺序从0开始编号(如tab键我们设置为123...,但m_radio的值仍是从0开始) 62 { 63 case 0: 64 m_nCalc = 10; 65 break; 66 case 1: 67 m_nCalc = 100; 68 break; 69 case 2: 70 m_nCalc = 1000; 71 break; 72 default: 73 break; 74 } 75 76 77 //将事件置为有信号 78 SetEvent(m_hEvent);//先拿到值再将事件置为有信号 79 80 } 81 82 83 void CMy0722ThreadTalkingDlg::OnDestroy() 84 { 85 CDialogEx::OnDestroy(); 86 87 // TODO: 在此处添加消息处理程序代码 88 m_bFlagQuit = false; 89 90 if(m_hThread) 91 { 92 if(WAIT_TIMEOUT == WaitForSingleObject(m_hThread,100)) 93 { 94 TerminateThread(m_hThread,-1); 95 } 96 CloseHandle(m_hThread); 97 m_hThread = NULL; 98 99 } 100 101 if(m_hEvent) 102 { 103 CloseHandle(m_hEvent); 104 m_hEvent = NULL; 105 } 106 }
2.消息
1 void CMy0722ThreadTalkingDlg::OnBnClickedButton1() 2 { 3 // TODO: 在此添加控件通知处理程序代码 4 //创建线程 5 if(!m_hThread) 6 { 7 m_hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);//在线程创建之前创建事件 8 //安全属性,(重置事件)人工为true,信号,名字); 9 m_hThread = (HANDLE)_beginthreadex(NULL,0,&CMy0722ThreadTalkingDlg::ThreadProc,this,0,NULL); 10 11 } 12 13 } 14 15 unsigned _stdcall CMy0722ThreadTalkingDlg::ThreadProc( void * lpvoid) 16 { 17 CMy0722ThreadTalkingDlg *pthis = ( CMy0722ThreadTalkingDlg *)lpvoid; 18 CString str; 19 while(pthis->m_bFlagQuit) 20 { 21 //等事件 22 if(WAIT_TIMEOUT == WaitForSingleObject(pthis->m_hEvent,100)) 23 { 24 continue; 25 } 26 int nSum = 0; 27 for(int i = 1;i <= pthis->m_nCalc;i++) 28 { 29 nSum+= i; 30 } 31 32 //第二种方式 消息--- 显示主窗口 33 pthis->PostMessage(UM_MSG,nSum,0); 34 38 39 } 40 return 0; 41 } 42 43 LRESULT CMy0722ThreadTalkingDlg::OnMsg(WPARAM wparam,LPARAM lparam) 44 { 45 CString str; 46 str.Format(_T("%d"),wparam); 47 48 GetDlgItem(IDC_EDIT1)->SetWindowText(str); 49 //while(1); 50 return 0; 51 } 52 53 void CMy0722ThreadTalkingDlg::OnBnClickedButton2() 54 { 55 // TODO: 在此添加控件通知处理程序代码 56 //计算 57 // m_radio = 1; 58 //UpdateData( FALSE); //初始化对话框数据 59 //获得要计算的值 60 UpdateData(TRUE); //从对话框上获得数据 61 switch (m_radio) 62 { 63 case 0: 64 m_nCalc = 10; 65 break; 66 case 1: 67 m_nCalc = 100; 68 break; 69 case 2: 70 m_nCalc = 1000; 71 break; 72 default: 73 break; 74 } 75 76 77 //将事件置为有信号 78 SetEvent(m_hEvent);//先拿到值再将事件置为有信号 79 80 } 81 82 83 void CMy0722ThreadTalkingDlg::OnDestroy() 84 { 85 CDialogEx::OnDestroy(); 86 87 // TODO: 在此处添加消息处理程序代码 88 m_bFlagQuit = false; 89 90 if(m_hThread) 91 { 92 if(WAIT_TIMEOUT == WaitForSingleObject(m_hThread,100)) 93 { 94 TerminateThread(m_hThread,-1); 95 } 96 CloseHandle(m_hThread); 97 m_hThread = NULL; 98 99 } 100 101 if(m_hEvent) 102 { 103 CloseHandle(m_hEvent); 104 m_hEvent = NULL; 105 } 106 }
https://www.cnblogs.com/itech/archive/2010/06/29/1767311.html
原文地址:https://www.cnblogs.com/curo0119/p/8352922.html
时间: 2024-11-13 06:49:28