Solved

Give it Some Time to Relax

Posted on 2000-03-23
7
196 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 75 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now