How to restrict Modal Dialog to one instance
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: How to restrict Modal Dialog to one instance

  1. #1
    Join Date
    Mar 2005
    Posts
    140

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,355

    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

  3. #3
    Join Date
    Mar 2005
    Posts
    140

    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.

    }

  4. #4
    Join Date
    Apr 2008
    Posts
    726

    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.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,243

    Re: How to restrict Modal Dialog to one instance

    Quote Originally Posted by reachb4 View Post
    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?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: How to restrict Modal Dialog to one instance

    Quote Originally Posted by reachb4 View Post
    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.

  7. #7
    Join Date
    Mar 2005
    Posts
    140

    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 05:13 AM.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,355

    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

  9. #9
    Join Date
    Mar 2005
    Posts
    140

    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.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,355

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center