Auto. start function after cdialog was shown?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Auto. start function after cdialog was shown?

  1. #1
    Join Date
    Jul 2001
    Posts
    293

    Auto. start function after cdialog was shown?

    Hello,

    I want to start a time intensiv function after showing a modal cdialog.
    How can I do this?

    Starting the function in OnInitDialog do not work because the dialog is not shown, then.

    Any ideas?

    tx
    Ralf

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

    Re: Auto. start function after cdialog was shown?

    If a function is "time intensiv" then create a worker thread and start it from there.
    See Using Worker Threads
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2001
    Posts
    293

    Re: Auto. start function after cdialog was shown?

    Hello,

    the problem is not the function.
    The problem is that I want to call it immediately after the dialog is shown.
    But I can not call the function from OnInitDialog, because in this case, the function is called before the dialog is shown.
    It is necessary, that OnInitDialog returns!

    Is there a function that is called by the system when the dialog was shown?

    thx
    Ralf

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

    Re: Auto. start function after cdialog was shown?

    OK, there is a well known trick to implement such a behaviour: PostMessage a user defined message (from WM_APP range or a registered message) from OnInitDialog to the dialog itself.
    Then in the message handler of this message call your function (or start a thread...)
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2001
    Posts
    293

    Re: Auto. start function after cdialog was shown?

    Hello Victor,

    I have not tried it, but isn't there a problem:
    WM_PAINT for the dialog will only processed when there is no other message in the message queue (says msdn).
    But when I post my message, it will be processed first. WM_PAINT (drawing the dialog) will processed after my function is executed. But that behaviour is bad for my app.

    Ralf

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

    Re: Auto. start function after cdialog was shown?

    Quote Originally Posted by Ralf Schneider View Post
    Hello Victor,

    I have not tried it, but isn't there a problem...
    Ralf, trying it (about one or two minutes) would have been at least one order faster than typing this post and waiting for reply (more than 30 minutes!)
    Victor Nijegorodov

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

    Re: Auto. start function after cdialog was shown?

    You could start in in another thread in OnInitDialog. Since it's in a thread, OnInitDialog will still return and the dialog still be shown.

    You may want to look into handling WM_SHOW.

    Another option is to set a short timer in OnInitDialog so that OnInitDialog returns, the window is displayed the timer goes off and your function gets executed.

  8. #8
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: Auto. start function after cdialog was shown?

    ...or can post a user-defined message from OnInitDialog.

    Example
    Code:
    // MyDialog.h
    
    class CMyDialog : public CDialog
    {
        enum {WM_POSTINITDIALOG = WM_APP + 1};
    
    // ...
    
        afx_msg LRESULT OnPostInitDialog(WPARAM wParam, LPARAM lParam);
    };
    Code:
    // MyDialog.cpp
    
    // ...
    
        ON_MESSAGE(CMyDialog::WM_POSTINITDIALOG, &CMyDialog::OnPostInitDialog)
    END_MESSAGE_MAP()
    
    BOOL CMyDialog::OnInitDialog()
    {
        CDialog::OnInitDialog();
    
        // ...
    
        PostMessage(CMyDialog::WM_POSTINITDIALOG);
        return TRUE;
    }    
    
    LRESULT CMyDialog::OnPostInitDialog(WPARAM wParam, LPARAM lParam)
    {
        // Here the dialog IS visible...
        return 0;
    }
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  9. #9
    Join Date
    Jul 2001
    Posts
    293

    Re: Auto. start function after cdialog was shown?

    Hello Ovidiu,

    I tried it.
    But it do not work because it seems, that the OnPaint-function of CDialog was not called before OnPostInitDialog.
    And that is my main problem.

    I have to think about a basical different solution.

    Ralf

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

    Re: Auto. start function after cdialog was shown?

    Quote Originally Posted by Ralf Schneider View Post
    ...
    I have to think about a basical different solution.

    Ralf
    There is nothing more "to think".
    See the post#2.
    Victor Nijegorodov

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,933

    Re: Auto. start function after cdialog was shown?

    Quote Originally Posted by Ralf Schneider View Post
    I tried it.
    But it do not work because it seems, that the OnPaint-function of CDialog was not called before OnPostInitDialog.
    And that is my main problem.

    I have to think about a basical different solution.
    A basically different solution, though the same well known one, is setting a timer.
    Best regards,
    Igor

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