Skip to main content
gamedev.net
🔒 Locked

Threading?

Started by rpreller Aug 4, 2006 at 4:14 AM 3 replies 750+ views
Original Post
rpreller
rpreller
Probably an easy question, I just am not sure how to go about it, since I've never done this before. Programming with Win32 API. I have a rather long function that takes about 3 to 5 minutes to finish calculation. I want the program to continue to respond to minimize, movement of the window, and the ability to cancel the function but stay inside the program. Preferably you hit a menu button, then a window pops up while the calculations are going with a cancel button. You hit cancel, and the function does its necessary cleanup and ends before it finished. Does this involve creating a DialogBox in a separate thread and upon EndDialog, I call SendNotifyMessage, my function polls for messages every once in a while, and when it gets the message it quits? Or am I completely wrong?
joanusdmentia
joanusdmentia
Keep your dialog in your main GUI thread and place the long calculation in the second thread. Using a boolean to signal the calculation to finish should be sufficient rather than using messages. But yeah, you're on the right track.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
rpreller
rpreller
It works almost to where I want it to.

Problem I'm having right now is I want the thread to message the cancel dialog box right before it finishes. This way the dialog box disappears on its own.

My code, which doesn't work, is as follows:

// Dialog Proc for Canceling CalcAO operationBOOL CALLBACK CalcCancel(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {		switch (message) {		case WM_INITDIALOG:			return TRUE;                case WM_APP:                        EndDialog(hDlg, 1);                        return TRUE;		case WM_COMMAND:			if (LOWORD(wParam) == IDC_CANCEL) {				CalcAOCancelFlag = true;				EndDialog(hDlg, LOWORD(wParam));				return TRUE;			}			break;		}		return FALSE;}DWORD WINAPI CalcThread( LPVOID lpParam ) {         /*.................excised code.............*/	SendNotifyMessage(HWND_BROADCAST,WM_APP,0,0);}DWORD dwThreadId;HANDLE hThread;hThread = CreateThread(NULL,0,CalcThread,0,0,&dwThreadId);CloseHandle(hThread);DialogBox(hInst,(LPCTSTR)IDD_CALC_CANCEL,this->hwnd,(DLGPROC)CalcCancel);


CalcCancel is able to set CalcAOCancelFlag to true, and in the excised code, CalcThread finishes as I want it to. But, when CalcThread finished normally, I want it to call SendNotifyMessage to CalcCancel to tell the dialog to end. This code does not cause the dialog to close when SendNotifyMessage is called.
Sandman
Sandman
You don't need to be using HM_BROADCAST. And if you did, you're still using it wrong - you should be creating a custom windows message to send your signal so every other application in your system will safely ignore it. And HM_BROADCAST only sends to top level windows - your cancel dialog appears to be a child window, which would explain why it's not getting the message. There are ways of getting around this using invisible server windows, but there's a much easier way in this case:

Create the dialog first and use the dialog's actual hWnd to send the message.
rpreller
rpreller
Thank you for the reply. I did happen to see that HWND_BROADCAST would be a bad way to do it, so I did retrieve the HWND of the dialog box, at least I think I did, but still could not get it to work. I'll get it eventually though I'm sure. Thanks again.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.