--- projs/trunk/projs/20161106_kicker/c_main.c 2016/11/07 03:39:51 78 +++ projs/trunk/projs/20161106_kicker/c_main.c 2016/11/07 17:06:44 79 @@ -1,4 +1,4 @@ -// $Header$ +//$Header$ //------------------------------------------------------------------------------------------------- //This file is part of "kicker", a program for spawning threads and/or processes periodically in //order to unstall certain Windows programs and scripts. The cause of the stall is unknown, but @@ -117,6 +117,8 @@ volatile int ctrl_c_or_break_pressed = 0; //volatile because the Visual C++ environment spawns a thread to handle //CTRL-C or CTRL-BREAK. +volatile int thread_ran = 0; //volatile because shared with thread. + BOOL CtrlHandler(DWORD fdwCtrlType) { @@ -135,11 +137,23 @@ //blow up and wipe out several city blocks! } + +//Thread function. Parameter ignored. +DWORD WINAPI MyThreadFunction(LPVOID lpParam) +{ + thread_ran = 1; + + return (0); +} + + int c_main(int argc, char* argv[]) { size_t selector = NELEM(kickish_words) - 1; size_t ui; int first_time = 1; + HANDLE thread_handle; + DWORD thread_id; emit_hline_std(); printf("kicker, Copyright 2016 David T. Ashley (dashley@gmail.com)\n"); @@ -190,7 +204,38 @@ //Print the description. printf("%s!", kickish_words[selector]); - //Delay. Sleep() is likely the most effective way to not waste CPU capacity. + //Start the thread. This will hopefully unstall things. + thread_handle = CreateThread( + NULL, // default security attributes + 0, // use default stack size + MyThreadFunction, // thread function name + 0, // argument to thread function + 0, // use default creation flags + &thread_id); // returns the thread identifier + + //If the create call failed, terminate. + if (thread_handle == NULL) + { + printf("\nOuch!\n"); + exit(0); + } + + //Wait for thread termination. This should be nearly instant. But it + //would seem to be the best way to wait so as not to chew up CPU + //cycles. + WaitForSingleObject(thread_handle, INFINITE); + + //At this point, the thread should have run, and the variable should have been + //set. If not, there is a real logical problem. + if (!thread_ran) + { + printf("\nDouble-ouch\n"); + exit(0); + } + + thread_ran = 0; + + //Delay. Sleep() is likely the most effective way to not waste CPU capacity. Sleep(1000); }