VISUAL BASIC PROBLEM

Problem Code VBPBGEN002

Description

sometime , when processing, my program stops responding.

VB Version Compatibility

This solution is tested in VB6 and expected to work in VB5 and VB4.

Theory

So many times you experience a situation that your program stops responding to :

  • User events such as Mouse-Click.

  • Keyboards.

Most of the time this happens due to lengthy process run by your program. Sometime we can not intervene the process and we have not alternative but to wait till process finishes. But we can intervene some lengthy process using <DoEvent> Keyword.

<DoEvent> keyword gives Window-Operating-System a opportunity to respond to other events. But bottleneck to this approach is that it increase the process time because it stops the process respond to other event and resume the process.

Solution/Example

  1. Open a new VB project
  2. Add a FORM , if not added automatically.
  3. Put a LABLE AND COMMAND button control on form.
  4. On Click() Event of COMMAND BUTTON write the following code.
Private Sub Command1_Click()
    Dim J As Long
    Dim M As Integer ' To be Used Later
    For J = 0 To 100000
        Label1.Caption = J
    Next
End Sub
  1. Run the project. FORM appears , press COMMAND BUTTON. you will notice that the LABEL ,which should display the value of J every time it changes, remains as it is. now try to minimizing form , you may feel that it does not respond to Mouse-Click. All this happens due to continues processing of the loop. once the loop finishes  the label shows 100000 and FORM minimized.
  2. Now replace the code from [FOR LOOP] till [NEXT] in above code with code below.
    For J = 0 To 100000
       Label1.Caption = J
       M = J Mod 1000
       If M = 0 Then
          DoEvents
       End If
    Next
  3. Run the project again , you will notice that LABLE show value of J into thousands. 1000,2000,3000 and so on , try to minimize the FORM, it will minimize this time. this all happens due to <DoEvents> keywords. Because after every 1000 iteration of loop VB give SYSTEM time to finish other process and come back to the loop. You can increase or decrease the occurance-frequency of <DoEvents> keyword according to your requirements.

NOTE :- Please read <DoEvents> in VB help to know in detail  advantage / disadvantage of this technique.

--------------------X-X-X-X-X-X-X-X--------------------

SPECIAL NOTE :- This tip is tested thoroughly. Use this tip is your own risk. Visual Code is not responsible for any damage caused directly or indirectly.

BACK

  1