Seeking multi-threaded console procedures under Win32 (C implementation)

xiaoxiao2021-03-06  72

#include #include

Bool bend = false;

Void fun (void *) {for (int i = 0; i <3; i ) Printf ("IM THREAD! / N"); bend = true;

Void main () {

_BEGINTHREAD (Fun, 0, NULL); while (! "{printf (" IM process! / n ");} Printf (" end of proGramm! / n ");

#include #include / * _beginthread, _endthread * / # include #include #include

Void Bounce (void * ch); Void CheckKey (Void * Dummy);

/ * Getrandom Returns a Random Integer Between Min and max. * / # Define getrandom (() (()% (IN) ((MAX) 1) - (MIN))) (min))

Bool repeat = true; / * Global repeat flag and video variable * / handle hstdout; / * handle for console window * / console_screen_buffer_info csbi; / * console information stand- /

Void main () {char ch = 'a';

HSTDOUT = GetStdHandle (std_output_handle);

/ * GET Display Screen's Text Row and Column Information. * / GetConsolescreenBufferInfo (HSTDOUT, & CSBI);

/ * Launch checkkey thread to check for terminating keyStroke. * / _Beginthread (CheckKey, 0, null);

/ * Loop until checkkey terminates program. * / While (repeat) {/ * on first loops, launch character threads. * / _Beginthread (bounce, 0, (void *) (CH ));

/ * WAIT One Second Between Loops. * / Sleep (1000L);}}

/ * CheckKey - Thread to wait for a keyStroke, Then clear repeat flag. * / Void checkkey {_getch (); repeat = 0; / * _ENDTHREAD IMPLIED * /

}

/ * Bounce - Thread to create and and control a colored letter that moves * around on the screen * * Params: ch - the letter to be moved * / void Bounce (void * ch) {/ * Generate letter and color attribute from. . thread argument * / char blankcell = 0x20; char blockcell = (char) ch; BOOL first = TRUE; COORD oldcoord, newcoord; DWORD result; / * Seed random number generator and get initial location * / srand (_threadid);. newcoord .X = getrandom (0, csbi.dwsize.x - 1); newcoord.y = getrandom (0, csbi.dwsize.y - 1); while (repeat) {/ * pause between loops. * / Sleep (100L) ;

. / * Blank out our old position on the screen, and draw new letter * / if (first) first = FALSE; else WriteConsoleOutputCharacter (hStdOut, & blankcell, 1, oldcoord, & result); WriteConsoleOutputCharacter (hStdOut, & blockcell, 1, newcoord, & result);

/ * Increment the coordinate for next placement of the block. * / OldCoord.x = newcoord.x; oldcoord.y = newcoord.y; newcoord.x = getrandom (-1, 1); newcoord.y = getrandom -1, 1);

/ * CORRECT Placement (and beep) if About to go off the screen. * / If (Newcoord.x <0) Newcoord.x = 1; else if (newcoord.x == csbi.dwsize.x) newcoord.x = Csbi.dwsize.x - 2; else if (newcoord.y <0) Newcoord.y = 1; else if (newcoord.y == csbi.dwsize.y) newcoord.y = csbi.dwsize.y - 2;

转载请注明原文地址:https://www.9cbs.com/read-121287.html

New Post(0)