您的位置:首页 > 编程语言 > C语言/C++

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

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 into
AfxBeginThread
function 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 function

public:
//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 Thread

2, Using AfxBeginThread with class member controlling function

3, Microsoft MSDN: AfxBeginThread

4, Microsoft MSDN example: CSocket::Attach

5, VC中创建新线程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息