Need help with multithreading in win32 c programming
I hope I can exlplain what my problem is without posting any code, as it is a complete mess right now. Disclaimer: I'm very new to win32 C programming.
I have a window that has a "batch" button on it. When the user clicks that button, it spawns off a process that loops through a file (sometimes a big one) and processes records from the file. If the user clicks "batch" and soon decides that he didn't want to do that, or the file is too big and it's just taking too long, I want him to be able to click a "cancel" processing button.
So I assumed the solution to this would be to create a modeless dialog box in a separate thread. The dialog box will show the progress of the processing of the file, and also have a "cancel processing" button that the user can click if he wants to stop the process. If the user clicks the "cancel processing" button, a global flag will be set, and the main processing window will check this flag before it processes each record to decide if it needs to continue or stop.
Now here's the problem. On the click of the "batch" button, I start a procedure in a new thread using _beginthreadex. This procedure creates the modeless dialog box and has a message loop in it that monitors any clicks on dialog box. But it doesn't appear that this procedure starts until all the messages on the main window that were triggered by the "batch" button click are finished. I run the program in debug mode in Visual Studio and put a breakpoint on the _beginthreadex line. I step line by line past it, and the dialog box doesn't appear. Only when I continue and it finishes the "batch" button process does the dialog box appear. Of course, I need for the dialog box to immediately appear after the call to _beginthreadex, or else the file processing will complete, and the user will have no chance to cancel it. Does a thread started by _beginthreadex not start immediately, and start sharing CPU time with the main thread? Does it only start once the process that issued the _beginthreadex (click of "batch" button) has completed?
Any help or advice on this would be greatly appreciated.