Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » Problem breaking loop (with close button) in main thread
Re: Problem breaking loop (with close button) in main thread [message #59036 is a reply to message #59034] Tue, 18 October 2022 20:09 Go to previous messageGo to previous message
Lance is currently offline  Lance
Messages: 534
Registered: March 2007
Contributor
On a second thought, your program needs the hMutex be acquired to continue its work. Then ProcessEvent() should be the way to go. Problem is, why is that ProcessEvent() failed you?

Can you try something like the following
DWORD GuiAcquireMutex(HANDLE hMutex, DWORD dwTimeout)
{
	if(hMutex == INVALID_HANDLE_VALUE)
		return ERROR_INVALID_HANDLE;
 
	DWORD     dwLastError = -1;

	while ( !g_bQuit && dwLastError != 0 )
	{
		dwLastError  = 0; // Success

		switch( WaitForSingleObject(hMutex, dwShortTimeout ) )
		{
		case WAIT_FAILED:
			Do_Set_LastError_Accordingly();
			break;
			
		case WAIT_TIMEOUT:
			dwLastError = ERROR_COUNTER_TIMEOUT;
			break;
		}
		// consider measure how long it takes ProcessEvents to finish,
		// if very quick, consider add a Sleep(500) or something like that
		// to avoid keeping CPU busy for nothing.
		topwin.ProcessEvents();
		if(TooSoon() )
		{
			Sleep (500);
		}
	}
	SetLastError (dwLastError) ;

	return dwLastError;
}


topwin is your TopWindow(or its derivative) object.

I am feeling your original code didnot prepare for the case when the WaitForSingleObject call actually succeeds. I might be wrong though. Above pseudo code will need to be tuned to reflect your true intention.

[Updated on: Tue, 18 October 2022 20:34]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Dealing with background tasks elegantly in a userinterface
Next Topic: error / memory leak in HttpServer example
Goto Forum:
  


Current Time: Sat Jun 15 17:41:54 CEST 2024

Total time taken to generate the page: 0.02242 seconds