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.