?
Solved

Give it Some Time to Relax

Posted on 2000-03-23
7
Medium Priority
?
204 Views
Last Modified: 2010-05-02
I often write code that force my computer to process a huge amount of data.  In these cases, the computer seems to allocate all its ressources to this hungry operation, forgetting to refresh the new screen display.
For instance, in the program I am currently building, I press a button which does the following tasks:
- Change my mousepointer to hourglass;
(screen.mousepointer = vbhourglass)
- Make a grid invisible;
(msgWhatever.visible = false)
- Process heavy data.

The problem is: the processor never allow my mouse cursor to change, nor the grid to become invisible.  I seek a clean and efficient way to give some processor time to these others simple visual tasks.  For now, I'm stuck with DOEVENTS, which works SOMETIMES.  Am I using this function the wrong way?
0
Comment
Question by:Sylvania
7 Comments
 
LVL 1

Accepted Solution

by:
pclement earned 225 total points
ID: 2650128
Doesn't sound like it. Sometimes DoEvents needs to be placed somewhat strategically in code so you may need to play a bit to see what works best.

If you are having repainting difficulties you also may want to try using the Refresh method prior to executing resource intensive code.

Ultimately DoEvents is a rather inefficient method for handling these type of problems, so be judicious in your usage.

0
 
LVL 14

Expert Comment

by:mcrider
ID: 2650150
"DoEvents" allows your program to dispatch any other outstanding tasks and then pickup where it left off.  You need to plan where you are putting "DoEvents" very carfully...  

For example, you could get into a situation where the user clicks a button to start a loop that has a "DoEvents" in it. While the loop is executing, if the user clicks the same button again, you will start 2 iterations of the loop... VERY BAD!

What you may also want to consider is calling the Refresh Event of controls that have it... for example, explicitly calling:

   Grid1.Visible = True
   Grid1.Refresh


Cheers!®©
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2650158
I could put down a long explanation, but an example is better.

Start a new project with two forms. But a command button on form2.

Paste the following in Form1
Option Explicit

Private Sub Form_Load()

Load Form2
Form2.Show vbModeless

End Sub


Paste the following in Form2
Option Explicit
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Sub Command1_Click()

Form2.Visible = False
'SetCursorPos (Form1.Left + Form1.Width / 2) / Screen.TwipsPerPixelX, (Form1.Top + Form1.Height / 2) / Screen.TwipsPerPixelY
Form1.MousePointer = vbHourglass
DoEvents
Dim lngCounter As Long
For lngCounter = 1 To 10 ^ 8
Next
Form1.MousePointer = vbDefault
Form2.Show vbModeless

End Sub


NB: Do keep the setcursorpos statement commented out for the moment.

You may have to adjust the value 10 ^ 8 according to the speed of your computer. Choose something that does something for a couple of seconds but doesn't leave you waiting for minutes ;-)


Start the program.

Click on Form2.

1 - Make sure that Form2 is positioned over Form1. Specifically, the command button should be well over Form1
Click the button. Everything's fine. The hourglass appears.

2 - Move Form2 well away from Form1 so there's no overlap. Click the button. Now the mousepointer does not change. Not even when you move the mouse over Form1.

3 - To fix this problem, uncomment the SetCursorPos statement.

I cannot say for definite, but maybe your problem is caused by something similar.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:caraf_g
ID: 2650175
The following would be even cleaner as the cursor seems to stay in the same location:

replace Form2 code with this:

Option Explicit
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Type POINTAPI
        x As Long
        y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long


Private Sub Command1_Click()

Dim cPos As POINTAPI
GetCursorPos cPos
Form2.Visible = False
SetCursorPos (Form1.Left + Form1.Width / 2) / Screen.TwipsPerPixelX, (Form1.Top + Form1.Height / 2) / Screen.TwipsPerPixelY
Form1.MousePointer = vbHourglass
DoEvents
SetCursorPos cPos.x, cPos.y
Dim lngCounter As Long
For lngCounter = 1 To 10 ^ 8
Next
Form1.MousePointer = vbDefault
Form2.Show vbModeless

End Sub
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2653641
From the lack of response I gather that that worked for you then?
0
 

Author Comment

by:Sylvania
ID: 2653825
No, I'm sorry.  My question wasn't about mouse cursor: it was about processor time dealing.  I was trying to find if someone had a better way to give some processor time to lighter tasks while heavy ones were going on their own; the mousepointer was only an example, like the msflexgrid.visible thing.
0
 

Author Comment

by:Sylvania
ID: 2654946
I guess this would shut this question.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question