我有两个CDialog类,一个线程类
在CDialog1中,我重载了OnOK(),然后在里面我
void CDialog1::OnOk()
{
CMncheck *CDck=new CMncheck; //在这我打开了第二个对话框
CDck->Create(IDD_MNCHECK);
CDck->ShowWindow(SW_SHOW); CWorkThread *pThread=new CWorkThread; //启动了一条线程
pThread->CreateThread(&m_BdCo);
pThread->Start();
}然后在CMncheck这个对话框里有几个slider,每个slider都对应一个
void CMncheck::OnCustomdrawSlrev(NMHDR* pNMHDR, LRESULT* pResult)
我想滑动Slider后把值直接传给CWorkThread线程,但我不想使用静态全局变量来实现传值,还有没有别的方法,望大家赐教!
希望能给出简短的代码
谢谢
不要只说类似 用消息 或只给出个 PostThreadMessage
请明示做法,以本题为例,给点代码,我自己好分析用分不够可以再加
在CDialog1中,我重载了OnOK(),然后在里面我
void CDialog1::OnOk()
{
CMncheck *CDck=new CMncheck; //在这我打开了第二个对话框
CDck->Create(IDD_MNCHECK);
CDck->ShowWindow(SW_SHOW); CWorkThread *pThread=new CWorkThread; //启动了一条线程
pThread->CreateThread(&m_BdCo);
pThread->Start();
}然后在CMncheck这个对话框里有几个slider,每个slider都对应一个
void CMncheck::OnCustomdrawSlrev(NMHDR* pNMHDR, LRESULT* pResult)
我想滑动Slider后把值直接传给CWorkThread线程,但我不想使用静态全局变量来实现传值,还有没有别的方法,望大家赐教!
希望能给出简短的代码
谢谢
不要只说类似 用消息 或只给出个 PostThreadMessage
请明示做法,以本题为例,给点代码,我自己好分析用分不够可以再加
m_WriteSoundThread->PostThreadMessage(WM_WRITESOUNDFILE_FILENAME,
0,(LPARAM)
(PWRITESOUNDFILE)&structWriteSoundFile);
there is an example in codeguru that hwo to use UI thread commnunication.
http://www.codeguru.com/multimedia/echosound.html
连PostThreadMessage都说出来了,还不会做,你到底要怎样?
pThread=new CWorkThread;后,把pThread的值保存起来。在 slider 的托动事件响应函数里使用pThread->PostThreadMessage(自定义消息,数据1,数据2);这样消息和数据就能被 thread 收到;你的自定义消息可以是任意数字,只要不是和系统消息重复即可。
一般来说 大于 WM_USER 的数字是安全的。你的thread又怎么得到消息和数据呢?
如果你的CWorkThread是从CWinThread继承的(估计你也没有别的花样)
那就把
BOOL PreTranslateMessage( MSG *pMsg );
加入你的 CWorkThread:
BOOL CWorkThread::PreTranslateMessage( MSG *pMsg )
{
if (pMsg->message == 自定义消息)
{
long v1= pMsg->wParam;//得到数据1
long v2= pMsg->lParam;//得到数据2
::MessageBox(0,"重要提醒","用消息传递,一次至多能发2个数据",MB_OK);
return TRUE;
}
else return FALSE;
}//打字太累了,你把分数给我加到200分去
非常感谢你的回答,我再鞠躬
不过我的线程不是从CWinThread继承来的,是个自己写的线程类CThread
你就在里面处理消息不就的了?原理一样。
如果你连这都不明白,那么你就把用 API 启动线程时要求提供的那个回调函数帖出来,
明天我给你看看怎么改。
今天我得回家了。
DWORD idThread, // thread identifier
UINT Msg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
#include <windows.h>
#include <stdio.h>
#include <conio.h>
unsigned long WINAPI Thread(PVOID pvoid);
void main()
{
DWORD dwThreadId;
printf("use timer in workthread of console application<masterz>\n");
HANDLE hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
Thread, // thread function
0, // argument to thread function
0, // use default creation flags
&dwThreadId);
DWORD dwwait=WaitForSingleObject(hThread,1000*30);
switch(dwwait)
{
case WAIT_ABANDONED:
printf("main thread WaitForSingleObject return WAIT_ABANDONED\n");
break;
case WAIT_OBJECT_0:
printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n");
break;
case WAIT_TIMEOUT:
printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n");
break;
}
CloseHandle(hThread);
_getch();
}unsigned long WINAPI Thread(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,NULL);
BOOL bRet;
int count =0;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
if(msg.message==WM_TIMER)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
if(count>4)
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here\n");
return 0;
}
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_THREAD_H__B7E64237_FE70_11D4_9C10_94A0307D5A3E__INCLUDED_)
#define AFX_THREAD_H__B7E64237_FE70_11D4_9C10_94A0307D5A3E__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000class CThread
{
public:
struct SThreadParam
{
CThread *m_pThreadObj;
LPVOID m_pParam;
};
public:
CWinThread *m_pWinThread;
protected:
SThreadParam m_sThreadParam; BOOL m_bIsDynamicallyCreated;
static BOOL bHasOperatorNewExecuted;
static CMutex mtx; volatile BOOL m_bCanAsynExit;
public:
CThread();
virtual ~CThread(); BOOL CreateThread(LPVOID pParam,
int nPriority = THREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);
inline BOOL SetPriority(int nPriority) const;
inline int GetPriority() const;
inline BOOL Start() const;
inline DWORD Suspend() const;
inline DWORD Resume() const;
BOOL DestroyThread(); //Dangerous, should be enhanced
inline BOOL GetExitCodeThread(DWORD &dwThreadExitCode) const;
virtual void AsynTerminateThread();
static UINT WaitFor(CThread *pThread, DWORD dwTimeout = INFINITE); void * operator new(size_t nSize);
void operator delete(void * pPointer);
#ifdef _DEBUG
void * operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
#endif
//! ! ! ! --N O T E-- ! ! ! ! !
//The following functions will be executed in the new thread.
//Please be noticed that all other member functions associated with
//these functions must NOT call any one of the member functions or
//any one of the data members declared above. //This is the main worker function.
//You should overload this function to implement thread actions.
//You can add any member functions in the derived class of CThread
//and call them inside Worker.
virtual UINT Worker(LPVOID pParam) = 0;
//This is the substitution for AfxEndThread.
//You MUST call this function to terminate a thread
//inside the thread instead of calling AfxEndThread.
void EndThread(UINT nExitCode); //This function acts as a sensor if you use the asynchronous thread
//exiting mechanism.
//It will return TRUE if father thread has demanded a thread ending.
BOOL SenseAsynExit();
//This function will be called when a thread is to be terminated.
virtual void OnExitingThread(); //!!!!!Please do not disturb the following functions and data members!!!!!
protected:
UINT ThreadStartsHere(LPVOID pParam);
UINT m_nReturnCode; friend UINT GeneralThreadFunction(LPVOID pParam);
};#define NBDNORMALTHREAD(ThreadClass,pParam) StartThread(new ThreadClass,pParam)CThread *StartThread(CThread *pThread,
LPVOID pParam = NULL,
int nPriority = THREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);////////////////////////////////////////////////////////
// Implementation of inline member functions
////////////////////////////////////////////////////////
BOOL CThread::Start() const
{
//Will cause assertion if thread is not created.
//If this message is traced, you must call CreateThread first.
ASSERT(m_pWinThread != NULL); return (m_pWinThread->ResumeThread() != 0xffffffff);
}DWORD CThread::Suspend() const
{
//Will cause assertion if thread is not created.
//If this message is traced, you must call CreateThread first.
ASSERT(m_pWinThread != NULL); return (m_pWinThread->SuspendThread() != 0xffffffff);
}DWORD CThread::Resume() const
{
//Will cause assertion if thread is not created.
//If this message is traced, you must call CreateThread first.
ASSERT(m_pWinThread != NULL);
return (m_pWinThread->ResumeThread() != 0xffffffff);
}BOOL CThread::SetPriority(int nPriority) const
{
//Will cause assertion if thread is not created.
//If this message is traced, you must call CreateThread first.
ASSERT(m_pWinThread != NULL); return m_pWinThread->SetThreadPriority(nPriority);
}int CThread::GetPriority() const
{
//Will cause assertion if thread is not created.
//If this message is traced, you must call CreateThread first.
ASSERT(m_pWinThread != NULL); return m_pWinThread->GetThreadPriority();
}BOOL CThread::GetExitCodeThread(DWORD &dwThreadExitCode) const
{
//Will cause assertion if thread is not created.
//If this message is traced, you must call CreateThread first.
ASSERT(m_pWinThread != NULL); return ::GetExitCodeThread(m_pWinThread->m_hThread, &dwThreadExitCode);
}#endif // !defined(AFX_THREAD_H__B7E64237_FE70_11D4_9C10_94A0307D5A3E__INCLUDED_)这是CThread里定义的所有东西
学习ing....... 我也.. --——0
|
|
{
CDialog * pDlg = (CDialog *)lParam; pDlg->Func1();
pDlg->Func2();
........
}void CDialog1::OnOk()
{
CMncheck *CDck=new CMncheck; //在这我打开了第二个对话框
CDck->Create(IDD_MNCHECK);
CDck->ShowWindow(SW_SHOW); CWorkThread *pThread= AfxBeginThread(ThreadProc,(LPVOID)this); //启动了一条线程
}
CWorkThread::CWorkThread();
CWorkThread::Create()
的代码贴出来。
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "Thread.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
//#define new DEBUG_NEW
#endif/////////////////////////////////////////////////////////////////////
//Global functions
/////////////////////////////////////////////////////////////////////
CThread *StartThread(CThread *pThread,
LPVOID pParam /* = NULL */,
int nPriority /* = THREAD_PRIORITY_NORMAL */,
UINT nStackSize /* = 0 */,
LPSECURITY_ATTRIBUTES lpSecurityAttrs /* = NULL */)
{
if (pThread)
{
pThread->CreateThread(pParam, nPriority, nStackSize, lpSecurityAttrs);
if (pThread->Start())
return pThread;
else
return NULL;
}
else
return NULL;
}
///////////////////////////////////////////////////////////////////
//Static members of CThread:
///////////////////////////////////////////////////////////////////BOOL CThread::bHasOperatorNewExecuted = FALSE;
CMutex CThread::mtx;UINT GeneralThreadFunction(LPVOID pParam)
{
CThread::SThreadParam *psThreadParam = (CThread::SThreadParam *)pParam;
UINT nRetCode = psThreadParam->m_pThreadObj->ThreadStartsHere(psThreadParam->m_pParam);
return nRetCode;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CThread::CThread()
{
m_sThreadParam.m_pParam = NULL;
m_sThreadParam.m_pThreadObj = this;
m_bIsDynamicallyCreated = bHasOperatorNewExecuted;
bHasOperatorNewExecuted = FALSE;
if (m_bIsDynamicallyCreated)
mtx.Unlock(); m_bCanAsynExit = FALSE;
m_pWinThread = NULL;
}CThread::~CThread()
{
if (m_pWinThread)
{
AsynTerminateThread();
WaitFor(this, 10000); delete m_pWinThread; //This may cause memory leak if thread is still running!!!
}
}void * CThread::operator new (size_t nSize)
{
mtx.Lock(); //It will be unlocked in the constructor
bHasOperatorNewExecuted = TRUE;
// mtx.Unlock();
void *p = malloc(nSize);
// TRACE("CThread object %x newed.\n",p);
return p;
}#ifdef _DEBUG
//For compatibility with DEBUG_NEW
void * CThread::operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
{
mtx.Lock(); //It will be unlocked in the constructor
bHasOperatorNewExecuted = TRUE;
// mtx.Unlock(); void *p = malloc(nSize);
// TRACE("CThread object %x newed.\n",p);
return p;
}
#endifvoid CThread::operator delete(void *pPointer)
{
free(pPointer);
// TRACE("CThread object %x deleted.\n",pPointer);
return;
}BOOL CThread::CreateThread(LPVOID pParam,
int nPriority /* = THREAD_PRIORITY_NORMAL*/,
UINT nStackSize /* = 0 */,
LPSECURITY_ATTRIBUTES lpSecurityAttrs /* = NULL */)
{
m_sThreadParam.m_pParam = pParam;
m_pWinThread = AfxBeginThread(GeneralThreadFunction,
(LPVOID)&m_sThreadParam,
nPriority,
nStackSize,
CREATE_SUSPENDED,
lpSecurityAttrs);
m_pWinThread->m_bAutoDelete = FALSE;
return (m_pWinThread != NULL);
}void CThread::AsynTerminateThread()
{
//Will cause assertion if thread is not created.
//If this message is traced, you must call CreateThread first.
ASSERT(m_pWinThread != NULL); m_bCanAsynExit = TRUE;
}BOOL CThread::DestroyThread()
{
//Will cause assertion if thread is not created.
//If this message is traced, you must call CreateThread first.
ASSERT(m_pWinThread != NULL); //Dangerous
//THIS MAY CAUSE MEMORY LEAK!!!
delete m_pWinThread;
m_pWinThread = NULL;
return TRUE;
}UINT CThread::WaitFor(CThread *pThread, DWORD dwTimeout /* = INFINITE */)
{
try
{
ASSERT(pThread->m_pWinThread != NULL); DWORD dwThreadCode;
if (!pThread->GetExitCodeThread(dwThreadCode))
throw 0;
if (dwThreadCode == STILL_ACTIVE)
{
::WaitForSingleObject(pThread->m_pWinThread->m_hThread, dwTimeout);
::GetExitCodeThread(pThread->m_pWinThread->m_hThread, &dwThreadCode);
}
return dwThreadCode;
}
catch(...) //If exception catched, it must be that pThread is an invalid pointer.
{
return 0;
}
}void CThread::OnExitingThread()
{
//Do nothing here.
}void CThread::EndThread(UINT nExitCode)
{
m_nReturnCode = nExitCode;
throw(this);
}BOOL CThread::SenseAsynExit()
{
//It is safe not to protect m_bCanAsynExit because there is only one thread
//can change its value, and there is only one thread reading it.
return m_bCanAsynExit;
}UINT CThread::ThreadStartsHere(LPVOID pParam)
{
UINT nReturnCode;
try
{
nReturnCode = Worker(pParam);
}
catch (CThread *p) //The pointer p is only a dummy.
{
nReturnCode = p->m_nReturnCode;
}
catch (...) //If any disaster happened, just throw it.
{
throw;
}
OnExitingThread();
// TRACE("Thread %x normally exited.\n",this);
return nReturnCode;
}这是CThread.cpp里面的所有代码
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_WORKTHREAD_H__F22281A4_1FE2_4DFB_8391_57D592604DAE__INCLUDED_)
#define AFX_WORKTHREAD_H__F22281A4_1FE2_4DFB_8391_57D592604DAE__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#include "Thread.h"class CWorkThread : public CThread
{
public:
CWorkThread();
virtual ~CWorkThread();
virtual UINT Worker(LPVOID pParam); // 重载work
CString strco,strhc,strno,strkw,strkm,strsm;
int i,co,hc,no,m_cotime,m_hctime,m_notime;
CString time,time1,time2,time3;
int m;
};#endif // !defined(AFX_WORKTHREAD_H__F22281A4_1FE2_4DFB_8391_57D592604DAE__INCLUDED_)这是CWorkThread.h
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "WorkThread.h"
#include "Gasbox6800.h"
#include "MahaDynoBench.h"
#include "RoundMeter.h"
#include "LEDBoard.h"
#include "Check.h"
#include "Mncheck1.h"
#include "DigitLEDGroup.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
///////////////////////////////////////////////////////////////////////*CString CWorkThread::strco="11.1";
CString CWorkThread::strhc="22.2";
CString CWorkThread::strno="33.3";
CString CWorkThread::strkw="44.4";
CString CWorkThread::strkm="55.5";
CString CWorkThread::strsm="66.6";
int CWorkThread::i=180;
int CWorkThread::co=1;
int CWorkThread::hc=1;
int CWorkThread::no=1;
int CWorkThread::m_cotime=0;
int CWorkThread::m_hctime=0;
int CWorkThread::m_notime=0;*/CWorkThread::CWorkThread()
{}CWorkThread::~CWorkThread()
{
}
UINT CWorkThread::Worker(LPVOID pParam)
{
CMncheck *DCk=(CMncheck *)pParam;
while(1)
{
//这里还有一部分代码我就不贴了
}
return true;
}
pThread->Create(&m_borad1);
pThread->Start();CWorkThread *pThread=new CWorkdThread; //第二个控件,我又起了一条
pThread->Create(&m_borad2);
pThread->Start();我感觉这样很浪费,有没有别的方法啊