CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 38
  1. #1
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624

    Controlling CWaitCursor from different methods

    Hello,
    I read some documentation on CWaitCursor and can't find my solution. Nor in the archives of this forum.
    I want to display an hourglass while some treatment is done.
    But the call of the treatment and its end are not in the same function.

    At start I send a message to another module, and want to start my CWaitcursor there.
    The application goes on, may do some other treatment.
    When I receive the answer, in the function where I treat the message I want to end the CWaitCursor.

    Should I create a separate thread whise work will only be to display an hourglass and kill it at the end of the treatment? Or is there a better way of doing it??

    Thanks for any hint

    Marina
    Please go vote for your country!

  2. #2
    Join Date
    Feb 2002
    Posts
    3,788

    Re: Controlling CWaitCursor from different methods

    so, what is it that's happening curently?
    BTW: I had a lot of problems displaying a CWait... in a thread.

  3. #3
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624

    Re: Controlling CWaitCursor from different methods

    I think I see the cursor change to hourglass, but it goes back to normal at the end of my method, i.e. when I send my message to the other module.

    I don't know how to make it last longer than the scope where it has been created;

    I use : or would like to do something like :

    Code:
     
    OnAction()
    {
     // Get my data
     RestoreWaitCursor();
    // Send my message
    }
    
    OnReceiveAnswerMessage()
    {
      // Do some treatment
      EndWaitCursor();
    }
    Please go vote for your country!

  4. #4
    Join Date
    Feb 2002
    Posts
    3,788

    Re: Controlling CWaitCursor from different methods

    this is from MSDN. maybe it helps:
    Code:
    void CMyView::AnotherFunction()
    {
       // some processing ...
    
       CMyDialog dlg;
       dlg.DoModal();
       RestoreWaitCursor();
    
       // some more processing ...
    }
    
    // If the dialog is invoked from a member function of
    // some non-CCmdTarget, then you can call CWinApp::DoWaitCursor
    // with a 0 parameter value to restore the hourglass cursor.
    void CMyObject::AnotherFunction()
    {
       CMyDialog dlg;
       dlg.DoModal();
       AfxGetApp()->DoWaitCursor(0); // same as CCmdTarget::RestoreWaitCursor   
    }

  5. #5
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624

    Re: Controlling CWaitCursor from different methods

    As I told you, I read MSDN , and that does not help.
    The examples given are always in the same scope.
    Please go vote for your country!

  6. #6
    Join Date
    Feb 2002
    Posts
    3,788

    Re: Controlling CWaitCursor from different methods

    are you dealing with threads?

  7. #7
    Join Date
    Aug 2002
    Location
    Covington, LA, USA
    Posts
    101

    Re: Controlling CWaitCursor from different methods

    If my assumption is correct that you want to show the hour glass until you get the message back, I would guess that BeginWait... in OnAction, and EndWait.. in OnMessagerecvd should do the trick provided of course that your program doesn't do something in the mean-time causing the wait cursor to go away, like opening a dialog. If it does you might need to introduce a helper-flag to be used to determin if the wait cursopr should be restored or not after such an action.
    "Soccer - it is not a matter of life and death - it's far more important than that" Bill Shankly

  8. #8
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624

    Re: Controlling CWaitCursor from different methods

    I don't think it works...
    I see the wait cursor very quickly, then nothing.
    And my function that stops the CWaitcursor is never called. I don't receive the answer yet.
    My guess is that the Wait Cursor is stopped when the program gets out of the scope where it has been created.

    Marina
    Please go vote for your country!

  9. #9
    Join Date
    Aug 2002
    Location
    Covington, LA, USA
    Posts
    101

    Re: Controlling CWaitCursor from different methods

    Something struck me while attempting to test my own reply:
    You don't have any OnSetCursor handlers anywhere that might override your hour glass when your method which sets it ends? I did...

    I've tested the underlying problem; Start wait cursor in one method, and ending it in another. It works fine (at least within the same class, as my test was). But; If some other class commands another cursor, that one wins... I'm not sure, but maybe some of the MFC base class message handler sets cursor "under the hood", and that is what causes your stuff not to work?

    This works fine (as long as I don't move the cursor outside CMainFrame):
    Code:
    void CMainFrame::OnNcRButtonDblClk(UINT nHitTest, CPoint point)
    {
    BeginWaitCursor();			// Display the hour-glass cursor
    CFrameWnd::OnNcRButtonDblClk(nHitTest, point);
    }
    
    void CMainFrame::OnNcRButtonDown(UINT nHitTest, CPoint point)
    {
    	EndWaitCursor();
    	CFrameWnd::OnNcRButtonDown(nHitTest, point);
    }
    "Soccer - it is not a matter of life and death - it's far more important than that" Bill Shankly

  10. #10
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624

    Re: Controlling CWaitCursor from different methods

    Well, yes, some other class do get the hand ... For example the communication class, that sends and receive the message, whereas I create my hourglass in the Dialog Class that request the messages and refreshed when the message eventually arrives.

    Even in the communication class, between the message is sent and its answer arrives, other messages may arrive that will cause another class to do the treatment.
    (That's normal coding isn't it?)

    Has nobody tried to make a CWaitCursor treatment like this?
    Marina
    Please go vote for your country!

  11. #11
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Controlling CWaitCursor from different methods

    Quote Originally Posted by Marina Vaillant
    I don't know how to make it last longer than the scope where it has been created;
    Simple: Don't create it locally (on the stack), but on the heap (with new), and keep a pointer to it as a class member. However, at that point you could just as well use SetCursor() instead. The trick about CWaitCursor is that you simply need to place it at the beginning of a block, and it's ctor and dtor will handle (even nested) SetCursor() calls for you. If you don't want that automatic functionality, just call SetCursor() (or BeginWaitCursor / EndWaitCursor) yourself.

  12. #12
    Join Date
    Aug 2002
    Location
    Covington, LA, USA
    Posts
    101

    Re: Controlling CWaitCursor from different methods

    Have you tried to NOT move the mouse until your anwer is received back in your dialog class? My theory is that in this case, you should have the hour glass until the answer processing is done (and Endwaitcursor is called). If that works, you need to handle WM_SETCURSOR in all your OTHER classes that might change the cursor, based on your desires originating from your dialog class. A possibly daunting task...
    "Soccer - it is not a matter of life and death - it's far more important than that" Bill Shankly

  13. #13
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Controlling CWaitCursor from different methods

    I have never dealt with the MFC class, nevertheless, using 'BeginWaitCurosr()' and 'EndWaitCursor()' usually works for me without any consideration of the stack or heap...

  14. #14
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624

    Re: Controlling CWaitCursor from different methods

    Quote Originally Posted by gstercken
    Simple: Don't create it locally (on the stack), but on the heap (with new), and keep a pointer to it as a class member. However, at that point you could just as well use SetCursor() instead. The trick about CWaitCursor is that you simply need to place it at the beginning of a block, and it's ctor and dtor will handle (even nested) SetCursor() calls for you. If you don't want that automatic functionality, just call SetCursor() (or BeginWaitCursor / EndWaitCursor) yourself.
    Ok, I'll try this.
    I allocate the CWaitCursor * pWaitCursor in my beginning function.

    Then I make a SetCursor(WHAT?) ??

    And then to stop it, a SetCursor(WHAT?), or a Delete(pWaitcursor) ??

    Presently, with an allocated CWAitCursor, and a delete (not called!!!) I have the cursor a ver short amount of time.
    I should not do that because if I never get a message as an answer, I'll never delete the pointer.

    Marina
    Please go vote for your country!

  15. #15
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624

    Re: Controlling CWaitCursor from different methods

    Quote Originally Posted by Andreas Masur
    I have never dealt with the MFC class, nevertheless, using 'BeginWaitCurosr()' and 'EndWaitCursor()' usually works for me without any consideration of the stack or heap...
    Don't you think Alin is right when s/he says that if another class or thread or whatever takes control of the Cursor, it gets back to normal even before my EndWaitCursor() is called ??

    Marina
    Please go vote for your country!

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured