Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redrawing a Win32 Interface 1

Status
Not open for further replies.

Dreadson

Instructor
Jun 11, 2003
63
0
0
US
I have a windows GUI program where you press a button, and it executes a looping code until you close it. However, there is no way for the user to close the win32 application (besides shutting it down in the windows task manager) because the interface goes white and does not respond. Is there a way to have this looping code execute, and still have a responsive GUI for when you want it to stop?
 
Create a new thread when the button is pressed, and have the loop in there. You need a global or static member function for the thread, and you do it like this:

HANDLE hThread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL);

The parameter after ThreadFunc (it is NULL above) should be a pointer to something your thread needs to run, it will be sent as type LPVOID to ThreadFunc, which looks like this:

DWORD WINAPI ThreadFunc(LPVOID lp)
{
// Do the loop here
return 0;
}

You should probably take measures to ensure that multiple threads are not created. Threads execute at the same time as GUI code, which can lead to problems if one thread is trying to access something at the same time as another thread. Multithreading can be incredibly complicated - I would suggest finding a good article about it with Google, as what I have said doesn't even scratch the surface. You should also look up mutexes - you may not have to use them, but there is a good chance you will.
 
Since he's using a button to start the looping, he could just disable the button after the thread's been launched. Maybe have a "stop/pause" and "start/resume" buttons for controlling the thread.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top