|
|
Home » U++ Library support » U++ MT-multithreading and servers » Problem breaking loop (with close button) in main thread
Problem breaking loop (with close button) in main thread [message #59034] |
Tue, 18 October 2022 17:19  |
awksed
Messages: 68 Registered: April 2012
|
Member |
|
|
Windows 7. U++ 16270. Multi-thread app.
I have a GuiAcquireMutex() function called from a main thread Paint() that calls
WaitForSingleObject(hMutex, dwShortTimeout) // dwShortTimeout = 1 second (full timeout is 60 seconds)
if the mutex is not acquired I call:
Ctrl::GuiSleep(1000) to allow other threads to run and if 60 seconds have not elapsed, jump
back to WaitForSingleObject().
I wish to allow the close button to call Close() (that sets boolean g_bQuit) and allow the user
close the app (breaking the mutex acquire loop).
Clicking the close button results in the windows message "... is not responding".
Adding Ctrl::ProcessEvents() causes a "WM_PAINT invoked ... while in Paint routine" error.
Is there some way in U++ to allow the close button to call Close() while the main thread is looping
(like Windows PumpWaitingMessages())?
Code:
DWORD GuiAcquireMutex(HANDLE hMutex, DWORD dwTimeout)
{
if(hMutex == INVALID_HANDLE_VALUE)
return ERROR_INVALID_HANDLE;
long long llNow;
long long llStart = GetMilliTime();
long long llEnd = llStart + (long long) dwTimeout;
DWORD dwShortTimeout = 1000; // 1 second
DWORD dwLastError;
DWORD dwWaitResult;
Again:
dwLastError = 0; // Success
dwWaitResult = WaitForSingleObject(hMutex, dwShortTimeout);
switch(dwWaitResult)
{
case WAIT_FAILED:
llNow = GetMilliTime();
if(llNow < llEnd)
{
if(g_bQuit)
{
dwLastError = ERROR_COUNTER_TIMEOUT;
SetLastError(dwLastError);
return dwLastError;
}
Ctrl::GuiSleep(1000); // Never sleeps for 1000 ms (always returns immediately)
goto Again;
}
dwLastError = GetLastError();
break;
case WAIT_TIMEOUT:
llNow = GetMilliTime();
if(llNow < llEnd)
{
if(g_bQuit)
{
dwLastError = ERROR_COUNTER_TIMEOUT;
SetLastError(dwLastError);
return dwLastError;
}
Ctrl::GuiSleep(1000); // Never sleeps for 1000 ms (always returns immediately)
goto Again;
}
dwLastError = ERROR_COUNTER_TIMEOUT;
SetLastError(dwLastError);
}
return dwLastError;
}
Thanks.
|
|
|
 |
|
Problem breaking loop (with close button) in main thread
By: awksed on Tue, 18 October 2022 17:19
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Tue, 18 October 2022 19:15
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Tue, 18 October 2022 20:09
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: awksed on Wed, 19 October 2022 01:11
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Wed, 19 October 2022 02:21
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Wed, 19 October 2022 02:42
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: awksed on Wed, 19 October 2022 20:56
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Thu, 20 October 2022 01:04
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Oblivion on Thu, 20 October 2022 20:10
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Thu, 20 October 2022 21:49
|
Goto Forum:
Current Time: Fri May 09 22:02:07 CEST 2025
Total time taken to generate the page: 0.02306 seconds
|
|
|