Using AfxBeginThread for multi-thread in VS C++
2016-10-06 11:30
585 查看
Summary
Two tricks about multi-thread
Controlling function must be global or static
Multithreading must be enabled by the application
Example
AfxBeginThread function
Code Example
Reference
2, APDURunDlg.cpp
2, Using AfxBeginThread with class member controlling function
3, Microsoft MSDN: AfxBeginThread
4, Microsoft MSDN example: CSocket::Attach
5, VC中创建新线程
Two tricks about multi-thread
Controlling function must be global or static
Multithreading must be enabled by the application
Example
AfxBeginThread function
Code Example
Reference
Summary
Create a separate thread as the worker thread, which can process time consuming task like printing, computation, etc., here I am using the new thread to process the interactive with the smart card, this mechanism can enable the application to response to user’s other action (requests to user interface), such as move the window, drag the scroll bar, etc.Two tricks about multi-thread
Controlling function must be global or static
Microsoft wants the controlling function either to be a global function or to be a static class member function, so you cannot access to your class member variables or methods from within the controlling function. This can be resolved by pass the application pointer intoAfxBeginThreadfunction as the 2nd parameter.
Multithreading must be enabled by the application
Remember to enable multithreading compiler option in the Visual Studio, otherwise this function will fail.Example
AfxBeginThread function
AfxBeginThread is to create new thread, I’m using two parameters, first to point to the controlling function for the worker thread, 2nd as the application pointer parameter to be passed to the controlling function.Code Example
1, APDURunDlg.h, to declare the worker thread functionpublic: //structure for passing to the controlling function typedef struct _MultiCardThreadParam { CWnd * wndHandler; } MultiCardThreadParam; MultiCardThreadParam m_mctParam; static UINT RunCardThread(LPVOID param); ......
2, APDURunDlg.cpp
void CAPDURunDlg::OnBnClickedRun() { // TODO: Add your control notification handler code here //call the thread on a button click, pass the current application pointer into the worker thread function. m_mctParam.wndHandler = this; AfxBeginThread(RunCardThread,(LPVOID)&m_mctParam); ......
UINT CAPDURunDlg::RunCardThread(LPVOID pVal) { MultiCardThreadParam * param = (MultiCardThreadParam *) pVal; CAPDURunDlg *dlg = (CAPDURunDlg *)param->wndHandler; //below code shows to access to the class member variables or methods from within the controlling function will require to use the "dlg" pointer passed in... //make sure the file to execute to be selected! if(dlg->m_edtDataSource.GetWindowTextLength() == 0 ) { AfxMessageBox(_T("Please select the file to execute!")); dlg->GetDlgItem(IDC_BROWSE)->SetFocus(); return 0; } ......
Reference
1, AfxBeginThread usage for creating a Mfc Worker Thread2, Using AfxBeginThread with class member controlling function
3, Microsoft MSDN: AfxBeginThread
4, Microsoft MSDN example: CSocket::Attach
5, VC中创建新线程
相关文章推荐
- C++ AMP: Changes in VS 11 Beta for C++ AMP
- Remote Thread Execution in System Process using NtCreateThreadEx for Vista & Windows7
- why it doesn't popup multi definitions error for method in .h class for C++?
- C++多线程——AfxBeginThread
- C++ AfxBeginThread
- 【C++】AfxBeginThread
- C++ AfxBeginThread的介绍/基本用法
- Strategies for Using C++ in Objective-C Projects (and vice versa)
- C++ AfxBeginThread1
- enumerateObjectsUsingBlock VS for(... in ...)
- ateObjectsUsingBlock 、for 、for(... in ...) 的区别 & 性能测试 for VS for(... in ...)
- mfc c++ 多线程AfxBeginThread 例子( 一)
- C++ AfxBeginThread和AfxEndThread 使用方法
- 关于afxbeginthread时WaitForMultipleObjects不起作用
- android multithread in c/c++ to call JNI
- Using UTF-8 as the internal representation for strings in C and C++ with Visual Studio
- c++多线程函数----AfxBeginThread
- Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using p
- AfxBeginThread与CreateThread的区别
- Visual C++ ActiveX Control for hosting Office documents in Visual Basic or HTML