Results 1 to 10 of 10

Thread: Give Time to Other Events in My Application

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Arrow Give Time to Other Events in My Application

    I've observed with c++/non VB apps that when they are doing some CPU intensive work they run quite fast and still respond to user events like mouse-click, keypress etc.
    Obviously they are not using any thing like the DoEvents of VB which processes the system message queue. Instead they seem to be giving time to only their own events.

    In VB, the only way I know to let your app respond to events is to put a DoEvents in your procedure/loop.

    Let's take a very simple example:
    VB Code:
    1. Option Explicit
    2. Dim bCancel As Boolean
    3.  
    4. Private Sub cmdCancel_Click()
    5.     If vbYes = MsgBox("Cancel Processing?", vbYesNo + vbQuestion) Then
    6.         bCancel = True
    7.     End If
    8. End Sub
    9.  
    10. Private Sub cmdOK_Click()
    11.     bCancel = False
    12.     Do
    13.         If bCancel Then Exit Do
    14.         DoSomething
    15.         DoEvents
    16.     Loop
    17.     MsgBox "OK", vbInformation
    18. End Sub
    19.  
    20. Private Sub DoSomething()
    21.     'Add code to do something here
    22. End Sub

    This code would work fine, but it is very slow. But if you comment out the Doevents, it would go very fast as it won't process the system message queue. But then it won't respond to cmdCancel_Click and you would ultimately have to End task teh application.
    I was looking for some replacement for the DoEvents which won't process the entire system message queue, instead give time to my application only.


    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Give Time to Other Events in My Application

    Don't you think if there were alternatives to DoEvents you would have heard all of us speak about it, and recommend it, by now?

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Give Time to Other Events in My Application

    I was wondering if we could make a dll in another language and implement it in VB applications instead of the DoEvents. I'm not sure how to do this though?

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Give Time to Other Events in My Application

    Quote Originally Posted by Pradeep1210
    I was wondering if we could make a dll in another language and implement it in VB applications instead of the DoEvents. I'm not sure how to do this though?

    Pradeep
    Now that could be quite possible (although I wouldn't know how to do it either).

    I suspect something in C++ might be done.

    Check your PMs.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Give Time to Other Events in My Application

    Moved to C/C++ at member's request. As the prior posts have indicated, he is looking for a possible C++ alternative to VB's DoEvents.

  6. #6
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Give Time to Other Events in My Application

    Moved back to General VB as requested.
    There's still a soft link to this thread in the C/C++ forum.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  7. #7

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Give Time to Other Events in My Application

    I found some hints

    This one is faster than the regular DoEvents.
    Though it still processes the entire message queue.

    Can someone guide me how to identify the messages related to my application only in the message queue?


    VB Code:
    1. Option Explicit
    2. Private Type POINTAPI
    3.     x As Long
    4.     y As Long
    5. End Type
    6.  
    7. Private Type MSG
    8.     hwnd As Long
    9.     message As Long
    10.     wParam As Long
    11.     lParam As Long
    12.     time As Long
    13.     pt As POINTAPI
    14. End Type
    15.  
    16. Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
    17. Private Declare Function TranslateMessage Lib "user32" (lpMsg As MSG) As Long
    18. Private Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As MSG) As Long
    19.  
    20. Private Const PM_REMOVE = &H1
    21.  
    22. Dim bCancel As Boolean
    23.  
    24. 'The alternative function for DoEvents:
    25. Private Sub MyDoEvents()
    26.     Dim CurrMsg As MSG
    27.     'The following loop extract all messages from the queue and dispatch them
    28.     'to the appropriate window.
    29.     Do While PeekMessage(CurrMsg, 0, 0, 0, PM_REMOVE) <> 0
    30.         TranslateMessage CurrMsg
    31.         DispatchMessage CurrMsg
    32.     Loop
    33. End Sub
    34.  
    35.  
    36. 'uses regular DoEvents
    37. Private Sub Command1_Click()
    38.     Dim lCounter As Long, nStart As Double
    39.     nStart = Timer
    40.     bCancel = False
    41.     For lCounter = 1 To 50000
    42.         If bCancel Then Debug.Print "Hey! it got Cancelled": Exit Sub
    43.         lblCounter.Caption = CStr(lCounter)
    44.         DoEvents
    45.     Next
    46.     Debug.Print "Old Doevents : "; Timer - nStart
    47. End Sub
    48.  
    49. 'uses new MyDoEvents (using API)
    50. Private Sub Command2_Click()
    51.     Dim lCounter As Long, nStart As Double
    52.     nStart = Timer
    53.     bCancel = False
    54.     For lCounter = 1 To 50000
    55.         If bCancel Then Debug.Print "Hey! it got Cancelled": Exit Sub
    56.         lblCounter.Caption = CStr(lCounter)
    57.         MyDoEvents
    58.     Next
    59.     Debug.Print "New Doevents : "; Timer - nStart
    60. End Sub
    61.  
    62. 'Signals a cancel
    63. Private Sub Command3_Click()
    64.     Debug.Print "I clicked Cancel"
    65.     bCancel = True
    66. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Give Time to Other Events in My Application

    Here' the form.

    Pradeep
    Attached Files Attached Files
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: Give Time to Other Events in My Application

    I havn't tried this, although it looks interesting, however I believe you should be able to simply specify your hwnd. i.e, change
    VB Code:
    1. Do While PeekMessage(CurrMsg, 0, 0, 0, PM_REMOVE) <> 0
    to
    VB Code:
    1. Do While PeekMessage(CurrMsg, Me.hwnd, 0, 0, PM_REMOVE) <> 0

  10. #10

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Give Time to Other Events in My Application

    Quote Originally Posted by anotherVBnewbie
    I havn't tried this, although it looks interesting, however I believe you should be able to simply specify your hwnd. i.e, change
    VB Code:
    1. Do While PeekMessage(CurrMsg, 0, 0, 0, PM_REMOVE) <> 0
    to
    VB Code:
    1. Do While PeekMessage(CurrMsg, Me.hwnd, 0, 0, PM_REMOVE) <> 0
    That increases the time taken

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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