void CASJSDlg::OnBnClickedOk()
{
//创建线程 //传递两个控件参数 m_list_file 为IDC_LIST控件变量,m_rich_compute 为 IDC_RICHEDIT
CWinThread *pThread = NULL;
pThread = AfxBeginThread(ThreadTest,&m_RichEdit_Log ,THREAD_PRIORITY_NORMAL,0,0,NULL);}
unsigned int CASJSDlg::ThreadTest(LPVOID pv)
{
//在新线程中使用m_list_file 与m_rich_compute 两个控件
...... ::AfxMessageBox (_T("threadtest"));
return 0;
}如何将m_list_file 与m_rich_compute 两个控件变量传递到新线程中,在新线程中对这两个控件进行操作?
AFX_THREADPROC pfnThreadProc,
LPVOID pParam,
int nPriority = THREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
DWORD dwCreateFlags = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);
CWinThread* AfxBeginThread(
CRuntimeClass* pThreadClass,
int nPriority = THREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
DWORD dwCreateFlags = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);pfnThreadProc
Points to the controlling function for the worker thread. Cannot be NULL. This function must be declared as follows: UINT __cdecl MyControllingFunction( LPVOID pParam );pThreadClass
The RUNTIME_CLASS of an object derived from CWinThread.pParam
Parameter to be passed to the controlling function as shown in the parameter to the function declaration in pfnThreadProc.nPriority
The desired priority of the thread. If 0, the same priority as the creating thread will be used. For a full list and description of the available priorities, see SetThreadPriority in the Windows SDK.nStackSize
Specifies the size in bytes of the stack for the new thread. If 0, the stack size defaults to the same size stack as the creating thread.dwCreateFlags
Specifies an additional flag that controls the creation of the thread. This flag can contain one of two values:CREATE_SUSPENDED Start the thread with a suspend count of one. Use CREATE_SUSPENDED if you want to initialize any member data of the CWinThread object, such as m_bAutoDelete or any members of your derived class, before the thread starts running. Once your initialization is complete, use CWinThread::ResumeThread to start the thread running. The thread will not execute until CWinThread::ResumeThread is called.0 Start the thread immediately after creation.lpSecurityAttrs
Points to a SECURITY_ATTRIBUTES structure that specifies the security attributes for the thread. If NULL, the same security attributes as the creating thread will be used. For more information on this structure, see the Windows SDK.
pThread = AfxBeginThread(ThreadTest,this ,THREAD_PRIORITY_NORMAL,0,0,NULL);
然后在线程中,先把你的指针转换成对话框对象的指针,然后根据对话框的指针,使用这两个控件就行了
CASJSDlg *pDlg = (CASJSDlg*)pv;以下就是你要的控件
pDlg->m_list_file
pDlg->m_rich_compute
你不是直接传的是一个窗口类对象的指针吗?pThread = AfxBeginThread(ThreadTest,this ,THREAD_PRIORITY_NORMAL,0,0,NULL);
Cpp文件里的代码如下
UINT CThreadDemoDlg::MyControllingFunction( LPVOID pParam )
{
MyDataStruct* pMds = (MyDataStruct*)pParam;
TRACE("第1个参数是 %d,第2个参数是 %d\n",pMds->iParam1,pMds->iParam2);
return 0;
}void CThreadDemoDlg::OnButton1()
{
// TODO: Add your control notification handler code here
m_mds.iParam1 = 10;
m_mds.iParam2 = 20;
CWinThread* pThread = AfxBeginThread(MyControllingFunction,&m_mds);
}在头文件再加上声明
struct MyDataStruct{
int iParam1;
int iParam2;
}; MyDataStruct m_mds;
static UINT MyControllingFunction( LPVOID pParam );
留意输出窗口,会输出,第1个参数是 10,第2个参数是 20
UINT CThreadDemoDlg::MyFunctionThisParam( LPVOID pParam )
{
CThreadDemoDlg* pWnd = (CThreadDemoDlg*)pParam;
TRACE("处理第1个数是 %d,处理第2个数是 %d,处理第3个数是 %d\n",pWnd->m_iVal1,pWnd->m_iVal2,pWnd->m_iVal3);
return 0;
}
void CThreadDemoDlg::OnButton2()
{
// TODO: Add your control notification handler code here
m_iVal1 = 100;
m_iVal2 = 200;
m_iVal3 = 300;
AfxBeginThread(MyFunctionThisParam,this);
}头文件如下
int m_iVal1;
int m_iVal2;
int m_iVal3;
static UINT MyFunctionThisParam( LPVOID pParam );输出结果,处理第1个数是 100,处理第2个数是 200,处理第3个数是 300