Solved

Give it Some Time to Relax

Posted on 2000-03-23
7
195 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
Comment Utility
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
Comment Utility
"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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
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
Comment Utility
From the lack of response I gather that that worked for you then?
0
 

Author Comment

by:Sylvania
Comment Utility
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
Comment Utility
I guess this would shut this question.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 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…

762 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

14 Experts available now in Live!

Get 1:1 Help Now