moi j’ai toujours un soucis sur createthread…
voilà j’ai acheté la bible du programmeur qui contenait un cdrom je programme sous visual c++ et la partie sur les thread deconne un peu…
il contient :
Ce fichier CPP:
#include <windows.h>
#include "generic.h"
HINSTANCE hInst; // Instance courante.
LPCTSTR lpszAppName = "MyApp";
LPCTSTR lpszTitle = "Mon Application";
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASS wc;
// Enregistrer la classe de fenêtre de l’application principale.
// …
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, lpszAppName );
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = lpszAppName;
wc.lpszClassName = lpszAppName;
RegisterClass( &wc );
hInst = hInstance;
// Créer la fenêtre principale.
//…
hWnd = CreateWindow( lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL,
NULL,
hInstance,
NULL
);
if ( !hWnd )
return( FALSE );
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
while( GetMessage( &msg, NULL, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
LRESULT CALLBACK About( HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return (TRUE);
case WM_COMMAND:
if ( LOWORD(wParam) == IDOK
|| LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, TRUE);
return (TRUE);
}
break;
}
return (FALSE);
}
////////////// ensuite ce fichier CPP
#include "genstub.cpp"
// C’est une procédure de thread fils qui attend un sémaphore,
// le garde cinq secondes et rel^che le sémaphore.
// Les threads qui ne peuvent accéder aux sémaphores attendront
// la fin des autres threads.
DWORD WINAPI ChildThreadProc( LPVOID parametre )
{
HWND hWnd= (HWND) parametre;
TCHAR szBuffer[256]; // tampon
long lSemCount = 0; // compteur du sémaphore
HANDLE hSemaphore = OpenSemaphore( SYNCHRONIZE, FALSE, “TEST_SEMAPHORE” );
wsprintf( szBuffer,“Thread %x en attente du sémaphore %x”,
GetCurrentThreadId( ), hSemaphore );
SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
// Attente du signalement du sémaphore
WaitForSingleObject( hSemaphore, INFINITE );
wsprintf( szBuffer,“Thread %x a obtenu le sémaphore”, GetCurrentThreadId( ) );
SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
Sleep( 5000 );
// Relâcher le sémaphore
ReleaseSemaphore( hSemaphore, 1, &lSemCount );
wsprintf( szBuffer,“Thread %x a terminé avec le sémaphore. Son compteur était %ld.”,
GetCurrentThreadId( ), lSemCount );
SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
CloseHandle( hSemaphore );
ExitThread( TRUE );
return 0;
}
// procédure de messages Windows
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HANDLE hSemaphore = 0;
switch (uMsg)
{
case WM_CREATE:
hSemaphore = CreateSemaphore( NULL, 4, 4, "TEST_SEMAPHORE" );
return DefWindowProc( hWnd, uMsg, wParam, lParam );
case WM_COMMAND: // traite les éléments de menu
switch ( LOWORD( wParam ) )
{
case IDM_TEST: // démarre un thread
{
DWORD dwChildId;
CreateThread( NULL, 0, ChildThreadProc, (LPVOID)hWnd, 0, &dwChildId );
}
break;
case IDM_EXIT:
DestroyWindow( hWnd );
break;
}
break;
case WM_USER:
{
// Message pour afficher les actions de synchronisation
TCHAR szBuffer[101];
static int row = 0;
static int msg_num = 1;
HDC hDC = GetDC( hWnd );
FillMemory( szBuffer, 100, 32 );
TextOut( hDC, 0, row, szBuffer, 100 );
wsprintf( szBuffer, "%3d: %s", msg_num++, (LPTSTR)lParam );
TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
if ( row > 200 )
row = 0;
else
row += 20;
ReleaseDC( hWnd, hDC );
}
break;
case WM_DESTROY:
if ( hSemaphore )
CloseHandle( hSemaphore );
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
return NULL;
}
////////le fichier d’entete:
#define IDM_EXIT 100
#define IDM_TEST 200
#define IDM_ABOUT 300
#define DLG_VERFIRST 400
#define DLG_VERLAST 404
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM);
le fichier RC n’est pas utile:
mais à la compilation
--------------------Configuration: test5 - Win32 Debug--------------------
Compiling…
genstub.cpp
Skipping… (no relevant changes detected)
Simple_Thread.cpp
Linking…
Simple_Thread.obj : error LNK2005: WinMain@16 already defined in genstub.obj
Simple_Thread.obj : error LNK2005: "long stdcall About(struct HWND *,unsigned int,unsigned int,long)" (?About@@YGJPAUHWND_@@IIJ@Z) already defined in genstub.obj
Simple_Thread.obj : error LNK2005: "char const * const lpszTitle" (?lpszTitle@@3PBDB) already defined in genstub.obj
Simple_Thread.obj : error LNK2005: "char const * const lpszAppName" (?lpszAppName@@3PBDB) already defined in genstub.obj
Simple_Thread.obj : error LNK2005: "struct HINSTANCE__ * hInst" (?hInst@@3PAUHINSTANCE__@@A) already defined in genstub.obj
Simple_Thread.obj : error LNK2001: unresolved external symbol "int __cdecl RegisterWin95(struct tagWNDCLASSA const *)" (?RegisterWin95@@YAHPBUtagWNDCLASSA@@@Z)
Debug/test5.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
test5.exe - 7 error(s), 0 warning(s)
merci de m’aider…