-
December 5th, 2009, 01:35 AM
#1
How to restrict Modal Dialog to one instance
Hello,
Please tell me on how to restrict a dialog window to only one instance.
Thanks in advance.
Regards,
J
-
December 5th, 2009, 04:26 AM
#2
Re: How to restrict Modal Dialog to one instance
Define "one instance" in your case.
Is it one instance of a dialog type application?
Or a modal dialog of some class in your application?
Or a modal dialog of any class (including or excluding MessageBox)?
Or ...?
And how about nested dialogs?
Victor Nijegorodov
-
December 5th, 2009, 05:12 AM
#3
Re: How to restrict Modal Dialog to one instance
It is a modal dialog of some class in your application.
Say, CPaintDlg::OnButtonClick()
{
CMyDialog myDlg;
myDlg.DoModal(); // this dialog needs to be controlled to one instance.
}
-
December 5th, 2009, 06:58 PM
#4
Re: How to restrict Modal Dialog to one instance
something quick-and-dirty would be to have a static int (initialised to zero) increment in the constructor, and decrement in the destructor. Only let DoModal() do anything when that int is == 1. if that int > 1, DoModal() would return without showing the dialogue form.
-
December 5th, 2009, 07:09 PM
#5
Re: How to restrict Modal Dialog to one instance
Originally Posted by reachb4
It is a modal dialog of some class in your application.
Say, CPaintDlg::OnButtonClick()
{
CMyDialog myDlg;
myDlg.DoModal(); // this dialog needs to be controlled to one instance.
}
By definition of modal only one instance of that dialog can be displayed at any one time.
So if you instantiate a CMyDialog and display it with a call to DoModal, then no other instance of that dialog or any other dialog can be displayed (as long as you don't display another instance of it within the CMyDialog code).
Keep in mind that what I've described is true only for modal dialogs and not for modeless dialogs. With modeless dialogs, you can have any number of an instance of the same modeless dialog being displayed at any one time. For modeless dialogs, you would need to right additional code to restrict its instantiation to a single instance.
At any rate, what problem are you trying to solve by restricting a given dialog to a single instance?
-
December 5th, 2009, 07:15 PM
#6
Re: How to restrict Modal Dialog to one instance
Originally Posted by reachb4
It is a modal dialog of some class in your application.
Say, CPaintDlg::OnButtonClick()
{
CMyDialog myDlg;
myDlg.DoModal(); // this dialog needs to be controlled to one instance.
}
You wouldn't be able to click any button outside of a modal dialog when it's running, so your question is unclear.
-
December 8th, 2009, 06:08 AM
#7
Re: How to restrict Modal Dialog to one instance
Hi,
Thanks for all those who co-operated.
This dialog is created inside a thread/hook. So the multiple instance. This dialog is displayed to show the the time a user spends with an application. User can cancel the dialog. If he don't then next time instead of a new dialog, existing dialog should display the newly calculated values.
I tried CreateMutex(...) in the constructor but tat is not working.
Regards,
Joe.
Last edited by reachb4; December 8th, 2009 at 06:13 AM.
-
December 8th, 2009, 06:28 AM
#8
Re: How to restrict Modal Dialog to one instance
If you meant the you spawn the dialog from a secondary thread then your design is not good. You should create windows only in the main GUI thread. Other threads should only inform/notify the main thread about some actions main thread has to do. The usual way for notifications - PostMessage some user defined or registered messages.
See Using Worker Threads
Victor Nijegorodov
-
December 8th, 2009, 10:58 AM
#9
Re: How to restrict Modal Dialog to one instance
Thanks,
But my program dont have a UI. Only UI it produces is the dialog i mentioned which will get activated when user presses alt+f4 on an application.
Regards,
Joe.
-
December 8th, 2009, 11:09 AM
#10
Re: How to restrict Modal Dialog to one instance
Then use the same technique as described in Avoiding Multiple Application Instances
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|