Threads unter Win32 API
-
Hi Leute,
ich bin eher programmierschwach und soll ein Programm schreiben, was einen Cursor innerhalb eines Rechtecks auf Tastendruck hin und her scheucht. Das ganze soll unter dem Passus "Multithreading" laufen. Ich habe mich mal versucht und das ist mein Code:
#include <iostream>
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>using namespace std;
typedef struct _tblock
{
int x_pos, y_pos, x_bew, y_bew;
}
TBlock;typedef struct _tbox
{
int links, oben, breite, hoehe;
}
TBox;TBlock block;
TBox box;void gotoxy(int x, int y)
{
{
COORD p = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), p );
}void Blinkerimplementierung();
{
for (int y = box.oben; y < box.oben + box.hoehe; y++)
for (int x = box.links; x < box.links + box.breite; x++)
{
gotoxy(x,y);
printf("%c",176);
}
}DWORD WINAPI block_bewegung (LPVOID Param);
{
gotoxy(box.links+block.x_pos, box.oben+block.y_pos);
printf("%c",176);block.x_pos += block.x_bew;
block.y_pos += block.y_bew;if (block.x_pos > box.breite-1) block.x_pos -= box.breite;
if (block.x_pos < 0) block.x_pos += box.breite;
if (block.y_pos > box.hoehe-1) block.y_pos -= box.hoehe;
if (block.y_pos < 0) block.y_pos += box.hoehe;gotoxy(box.links+block.x_pos, box.oben+block.y_pos);
printf("%c",219);Sleep(70);
return ;
}
int main();
DWORD ThreadId;
HANDLE ThreadHandle;
VOID ExitThread(DWORD status);{
char taste;block.x_pos = 0;
block.y_pos = 0;
block.x_bew = 0;
block.y_bew = 0;box.links = 15;
box.oben = 5;
box.breite = 25;
box.hoehe = 15;Blinkerimplementierung();
gotoxy(0,0);
printf("Richtung: Hoch | Runter | Links | Rechts Halt: Leertaste Exit: Esc");
CreateThread(NULL, 0, block_bewegung, NULL, 0, NULL);{
taste = getchar();
if (taste == 27)
{
ExitThread(DWORD status);
}if (taste==-32)
{
taste = getchar();
switch (taste)
{
case 75:
block.x_bew = -1;
block.y_bew = 0;
break;
case 77:
block.x_bew = 1;
block.y_bew = 0;
break;
case 72:
block.x_bew = 0;
block.y_bew = -1;
break;
case 80:
block.x_bew = 0;
block.y_bew = 1;
break;
}
}
if (taste==32)
{
block.x_bew = 0;
block.y_bew = 0;
}}
gotoxy(0,20);
return ;
}
}#include <iostream>
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>using namespace std;
typedef struct _tblock
{
int x_pos, y_pos, x_bew, y_bew;
}
TBlock;typedef struct _tbox
{
int links, oben, breite, hoehe;
}
TBox;TBlock block;
TBox box;void gotoxy(int x, int y)
{
{
COORD p = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), p );
}void Blinkerimplementierung();
{
for (int y = box.oben; y < box.oben + box.hoehe; y++)
for (int x = box.links; x < box.links + box.breite; x++)
{
gotoxy(x,y);
printf("%c",176);
}
}DWORD WINAPI block_bewegung (LPVOID Param);
{
gotoxy(box.links+block.x_pos, box.oben+block.y_pos);
printf("%c",176);block.x_pos += block.x_bew;
block.y_pos += block.y_bew;if (block.x_pos > box.breite-1) block.x_pos -= box.breite;
if (block.x_pos < 0) block.x_pos += box.breite;
if (block.y_pos > box.hoehe-1) block.y_pos -= box.hoehe;
if (block.y_pos < 0) block.y_pos += box.hoehe;gotoxy(box.links+block.x_pos, box.oben+block.y_pos);
printf("%c",219);Sleep(70);
return ;
}
int main();
DWORD ThreadId;
HANDLE ThreadHandle;
VOID ExitThread(DWORD status);{
char taste;block.x_pos = 0;
block.y_pos = 0;
block.x_bew = 0;
block.y_bew = 0;box.links = 15;
box.oben = 5;
box.breite = 25;
box.hoehe = 15;Blinkerimplementierung();
gotoxy(0,0);
printf("Richtung: Hoch | Runter | Links | Rechts Halt: Leertaste Exit: Esc");
CreateThread(NULL, 0, block_bewegung, NULL, 0, NULL);{
taste = getchar();
if (taste == 27)
{
ExitThread(DWORD status);
}if (taste==-32)
{
taste = getchar();
switch (taste)
{
case 75:
block.x_bew = -1;
block.y_bew = 0;
break;
case 77:
block.x_bew = 1;
block.y_bew = 0;
break;
case 72:
block.x_bew = 0;
block.y_bew = -1;
break;
case 80:
block.x_bew = 0;
block.y_bew = 1;
break;
}
}
if (taste==32)
{
block.x_bew = 0;
block.y_bew = 0;
}}
gotoxy(0,20);
return ;
}
}Diese Zeile scheint i-wie verkehrt zu sein:
ExitThread(DWORD status);
Kann mir jemand helfen? Ich wollte das hin und her des Cursors durch Tastendruck "Esc" beenden.
-
ohne cpp-tags wird sich das wohl niemand durchlesen. wenn du deinen quellcode lesbar machst, hast du wenigstens ne chance
-
was einen Cursor innerhalb eines Rechtecks auf Tastendruck hin und her scheucht. Das ganze soll unter dem Passus "Multithreading" laufen.
Wer bitte vergibt solche Aufgaben ???
Multithreading macht nur "Sinn", wenn:
- Du langwierige Jobs von der Oberflaeche entkoppeln willst,
- langwierige Jobs nicht durch kontinuierliches Unterbrechen und Fortsetzen im Code verschandeln willst,
- oder mehrere Kerne nutzen willst / musst.Bei dem beispiel ist nix dabei ^^
Meistens hasst noch das Problem, das deine "GUI" Bibliothek aufgrund einfachem (performantem) Event Implementeirung gar nicht Multithreading faehig ist, also Du gar nicht mit einem anderen thread GUI funktionen aufrufen darfst !
Was du verwendest sieht aus wie ne curses Biblio ... keine Ahnung wo du deren Funktionen einblendest, den Header seh ich ned ^^
Und bist dir sicher das die multithreaded verwendet werden kann ?Du umgehst das eigentliche Problem des multithreadings, der gemeinsammen nutzung der Daten <sarkasmus on> elegant <sarkasmus off> mit globalen variablen ^^
Das die schlechteste Loesung die man haben kann ^^
Und nicht mal geschuetzt. Wenn dein Programm so mal laufen sollte, wird es an und ab paar ganz putzige nicht nachvollziehbare DInge tun ^^ Wenn Glueck hasst, nur auf ganz speziellen Platformen (race conditions)Mein Tipp:
Fang multithreading mit was anderen an als mit zeugs was sofort auf die GUI zurueckschreiben will. Irgend ne langwierige berechnung .. oder dateien konvertieren oder sowas ...Threading / Multiprozessing / assynchronitaet generell sind eher (sprachunabhaengig) komplexere Themen der Programmierung. Da sollt man sich ranwagen, wenn man richtig sattelfest beim rest ist.
Es gibt viele Loesungen die sehen einfach aus, aber zu verstehen iss ne andere Sache ^^Und wie gesagt, threads die nur auf andere Threads warten und selber nix langwieriges machen schaden mehr als das sie nuetzen ...
Ciao ...