קוד:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
/* #include <strsafe.h> */
#define MY_BUFSIZ 255
#define WHEN_TO_STOP 10
DWORD WINAPI MyThread( LPVOID lpParam )
{
int *pInt;
HANDLE hStdout;
/*
TCHAR buf[MY_BUFSIZ];
size_t cchStringSize;
DWORD dwChars;
*/
/*
hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
if( hStdout == INVALID_HANDLE_VALUE ) {
return 1;
}
*/
pInt = ( int* )lpParam;
for( ;; ) {
if( *pInt == WHEN_TO_STOP ) {
return 0;
}
++(*pInt);
/*
StringCchPrintf( buf, MY_BUFSIZ, TEXT( "test is: %d\t" ), *pInt );
StringCchLength( buf, MY_BUFSIZ, &cchStringSize, NULL );
WriteConsole( hStdout, buf, cchStringSize, &dwChars );
*/
printf( "test is: %d\t", *pInt );
Sleep( 1000 );
}
return 0;
}
int main( int argc, char **argv )
{
int test = 0;
DWORD dwThreadID;
HANDLE hThread;
hThread = CreateThread(
NULL,
0,
MyThread,
&test,
0,
&dwThreadID
);
if( hThread == NULL ) {
fprintf( stderr, "Failed to create the thread (errid: %d)\n", GetLastError() );
return EXIT_FAILURE;
}
/*
* Your program continues here;
*/
printf( "Waiting for thread to finish\n" );
WaitForSingleObjectEx( hThread, INFINITE, 0 );
return EXIT_SUCCESS;
}