Threads mit der MFC

Ich habe versucht mit der Funktion AfxBeginThread einen neuen
(Worker-)Thread zu starten und erhalte vom Compiler den Fehler

„AfxBeginThread’ : Durch keine der 2 Ueberladungen kann Parameter 1 vom Typ ‚unsigned int (void)‘ konvertiert werden“

Welche Parameter muss ich der Funktien übergeben um den Thread zu starten?

Danke im voraus, Daniel

Hallo Daniel!

Du musst als ersten Paramter einmal einen Pointer auf die Funktion, die deinen Thread repräsentieren soll, übergeben. Eine detailierte Information erhältst du in der MSDN:

_**CWinThread* AfxBeginThread( 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 );**

Return Value:
Pointer to the newly created thread object.

Parameters:

pfnThreadProc:
Points to the controlling function for the worker thread. Cannot be NULL. This function must be declared as follows:

UINT 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, seeSetThreadPriority in the Win32 Programmer’s Reference.

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. The thread will not execute until 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 Win32 Programmer’s Reference._

Achtung! Du kannst keine Funktion als „Thread-Funktion“ angeben, welche einen nicht-statische Methode einer Klasse ist!

Ich hoffe, ich konnte helfen,
KoRn!

Hi Daniel!

Welche Parameter muss ich der Funktien übergeben um den Thread
zu starten?

AfxBeginThread ( RUNTIME_CLASS(CDeineEigeneKlasse) );

Wobei die Klasse CDeineEigeneKlasse eine von CWinthread abgeleitete Klasse ist. Die erzeugst du im VisualC++ 5 und 6 indem du mit der rechten Maustaste im KlassenFenster auf „Neue Klasse“ klickst, dort dann den Namen für deine Klasse eingibst und schliesslich als Basisklasse „CWinThread“ wählst.

Diese Klasse enthält dann eine Funktion die heisst:
BOOL CDeineEigeneKlasse::InitInstance()
Diese Funktion wird als alle erste aufgerufen und in der kannst du deine ThreadArbeiten erledigen.
In der Funktion ExitInstance() kannst du deinen Speicher wieder freigeben oder was auch immer du beim beenden des Threads benötigst.

Ciao
Catmad