How could I block or queue a call to RunModalLoop() if a CWnd is already in a Modal State?

Hi, I have a CWnd derived class, say m_Wnd, that has a function foo() which calls RunModalLoop().  RunModalLoop() is used to pause the foo() function until a certain task is completed.  When this task is completed it will end RunModalLoop() and the rest of foo() will finish.  The user can make many calls to  m_Wnd->foo(), but a problem occurs when a previous call is still in RunModalLoop() and a subsequent call is made to RunModalLoop() because  m_Wnd is already in a modal state.  Is there a way to queue these calls to  foo() or RunModalLoop()?  I have beginner knowledge in threading..does this require the use of a semaphore?

Thanks
LVL 1
DrivenXAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
static bool bInProcess = false;
if(bInProcess) return;

bInProcess = true;

do long process here - run moodal loop

bInProcess = false;
0
DrivenXAuthor Commented:
Thanks for the quick reply AndyAinscow.  Sorry, I  should have not asked for a blocking solution in the title.. I have something similar implemented, but what Im truly looking for is a way to queue these calls.  Sorry about that, Thanks.
0
AndyAinscowFreelance programmer / ConsultantCommented:
OK.  No problems.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

jkrCommented:
What are the different parameters for the modal loops?
0
DrivenXAuthor Commented:
Sorry, dont quite understand the question jkr.  RunModalLoop() does not take any parameters.

foo() takes in a URL and generates a thumbnail image of that web page. The web page is loaded in a webbrowser control and the image is captured from there.  RunModalLoop() is used to wait until the browser control is finished loading the page before capturing the web page as an image.  When the page finishes loading, RunModalLoop() quits.

If the user makes many quick successive to foo(), their will be a conflict with the RunModalLoops.  Hope that helps..
0
jkrCommented:
>>RunModalLoop() does not take any parameters.

Yes, but when you want to "queue these calls to  foo()" there have to be some differences in the setup for these. Otherwise, you could just increment a counter and execute 'RunModalLoop()' n times until it drops to zero...
0
DrivenXAuthor Commented:
What would be a way to implement this counter?

The problem is, when one call to RunModalLoop() is running, another call cannot made to it because CWnd is already in a modal state.  A second call to RunModalLoop() causes an ASSERT error ( ASSERT(!(m_nFlags & WF_MODALLOOP)); // window must not already be in modal state ) if the previous call has not finished yet.
0
jkrCommented:
Good question. How's your "blocking solution" set up so far? You can increment when rejecting, the problem is probably communicating the new value.
0
DrivenXAuthor Commented:
jkr: It is very similar to AndyAinscow's suggestion.  Would a list storing the parameters of the queued calls to foo() be viable?  When one call to foo() finishes, it will check if there are any more calls waiting.

But if possible, Id rather use a built in mechanism..
When a user activates an event handler (eg. generate thumbnail), it will invoke a handler function.  Does this handler function run in a separate thread? If the user activates the same handler function a second time will this run in another thread? Is it possible to use a mutex/semaphore in this case?

I have tried adding a CCriticalSection member variable to m_Wnd class and locking inside foo() but it did not work..
0
jkrCommented:
Hm, getting back to the original issue, I'd rather go for the 'blockin and counting' approach (i.e. count the messages and process them later).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DrivenXAuthor Commented:
Thanks, i needed to create a list to store the parameters as they are different for each call.  But seems to be working :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.