Solved

Give it Some Time to Relax

Posted on 2000-03-23
7
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create a duplicate finder Application 9 127
VBA/SQL - Connect to SQL server and pull data 4 137
Using "ScreenUpdating" 6 84
Excel Automation VBA 19 104
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

726 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