Pb thread ss classe

robbynaze Messages postés 11 Date d'inscription vendredi 6 février 2004 Statut Membre Dernière intervention 23 novembre 2004 - 25 juin 2004 à 10:16
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019 - 25 juin 2004 à 10:42
bonjour à tous, j'ai le problème suivant :
l'utilisation des threads ss le main ne pause aucun problème, mais lorsque j'essaye de l'intégrer dans une classe (pour acquisition de données sur port série), j'ai le message d'erreur suivant :
error C2664: 'Attach' : cannot convert parameter 1 from 'unsigned long (void *)' to 'unsigned long (__stdcall *)(void *)'

l'utilisation d'une méthode de la classe en tant que routine attachée au thread est-elle possible ? merci de votre aide ...

sources :
test.cpp
//------------------------------------------------------------------------------
// Test
#include "stdafx.h"
#include "test.h"
#include <conio.h>
#include "cThread.h"

CWinApp theApp;
using namespace std;
DWORD WINAPI fThread2( LPVOID );

// cAcquisition
class cAcquisition {
public:
cThread Thread1;
cAcquisition();
virtual ~cAcquisition();
Start();
Stop();
DWORD WINAPI fThread1( LPVOID ); };

//------------------------------------------------------------------------------
// fThread2
DWORD WINAPI fThread2( LPVOID )
{
while(1) { printf("\nfThread_Boucle2"); Sleep(1000); };
}

//------------------------------------------------------------------------------
// cAcquisition
cAcquisition::cAcquisition() {};

cAcquisition::~cAcquisition(){};

cAcquisition::Start() {
Thread1.Attach(fThread1);
Thread1.Start();
};

cAcquisition::Stop() {
Thread1.Stop(); };

DWORD WINAPI cAcquisition::fThread1( LPVOID ) {
while(1) { printf("\nfThread1"); Sleep(1000); } };

//------------------------------------------------------------------------------
// main
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
cAcquisition Acquis;
cThread Thread2;

if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
printf("erreur init afx"); return 1; }

Acquis.Start();
Thread2.Attach(fThread2);
Thread2.Start();
while(!kbhit()) {}
Thread2.Stop();
Acquis.Stop ();

return 0;
}

test.cpp
//-------------------------------------------------------------------------------
// cThread.h

#ifndef __CTHREAD_H__
#define __CTHREAD_H__
#include <windows.h>

class cThread
{
public:
cThread() { m_pThreadFunc = cThread::EntryPoint;}

cThread(LPTHREAD_START_ROUTINE lpExternalRoutine) { Attach(lpExternalRoutine); }

~cThread() { if ( m_ThreadCtx.m_hThread ) Stop(true); }

DWORD Start( void* arg = NULL ) {
m_ThreadCtx.m_pUserData = arg;
m_ThreadCtx.m_hThread = CreateThread(NULL, 0, m_pThreadFunc, this, 0, &m_ThreadCtx.m_dwTID);
m_ThreadCtx.m_dwExitCode = (DWORD)-1;
return GetLastError(); }

DWORD Stop ( bool bForceKill = false ) {
if ( m_ThreadCtx.m_hThread ) {
GetExitCodeThread(m_ThreadCtx.m_hThread, &m_ThreadCtx.m_dwExitCode);
if ( m_ThreadCtx.m_dwExitCode == STILL_ACTIVE && bForceKill )
TerminateThread(m_ThreadCtx.m_hThread, DWORD(-1));
m_ThreadCtx.m_hThread = NULL; }
return m_ThreadCtx.m_dwExitCode; }

DWORD GetExitCode() const {
if ( m_ThreadCtx.m_hThread )
GetExitCodeThread(m_ThreadCtx.m_hThread, (LPDWORD)&m_ThreadCtx.m_dwExitCode);
return m_ThreadCtx.m_dwExitCode; }

void Attach( LPTHREAD_START_ROUTINE lpThreadFunc ){
m_pThreadFunc = lpThreadFunc; }

void Detach( void ){
m_pThreadFunc = cThread::EntryPoint; }

protected:
static DWORD WINAPI EntryPoint( LPVOID pArg) {
cThread *pParent = reinterpret_cast<cThread*>(pArg);
pParent->ThreadCtor();
pParent->Run( pParent->m_ThreadCtx.m_pUserData );
pParent->ThreadDtor();
return STILL_ACTIVE; }

virtual DWORD Run( LPVOID /* arg */ ) { return m_ThreadCtx.m_dwExitCode; }
virtual void ThreadCtor(){ }
virtual void ThreadDtor(){ }

private:
class cThreadContext {
public:
cThreadContext(){ memset(this, 0, sizeof(this));}

public:
HANDLE m_hThread; // The Thread Handle
DWORD m_dwTID; // The Thread ID
LPVOID m_pUserData; // The user data pointer
LPVOID m_pParent; // The this pointer of the parent cThread object
DWORD m_dwExitCode; // The Exit Code of the thread
};

protected:
cThreadContext m_ThreadCtx; // The Thread Context member
LPTHREAD_START_ROUTINE m_pThreadFunc; // The Worker Thread Function Pointer
};

#endif //__CTHREAD_H__

1 réponse

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
25 juin 2004 à 10:42
ENCORE....
Declarer fonction static car systeme doir connaitre son adresse a la compilation.

ciao...
BruNews, Admin CS, MVP Visual C++
0
Rejoignez-nous