Link to home
Start Free TrialLog in
Avatar of Jasminshah
Jasminshah

asked on

How to keep continuing the loop if i click on Menu?(Multithreading)

I am getting problem when i wrote a loop which is given below:
suppose in a form i have one menu(mnuShow) & one button(cmdstart)

dim i as variant
private sub cmdstart_click()
i=0
while true  'this is infinite loop
  debug.print i
  i=i+1
  DoEvents
wend

end sub

now when i focus on menu it stops the loop which is running.My problem is it should not stopped the loop
even if i focus on menu...(and i also want menu enabled)
Do u have any solution?
i will be thankfull to u?plz. try soon...




 
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Hi,

How about putting the Codes in a Module, run it when the program starting?

Example: Put the code in a Sub called Main() , goto Project> Project Property> Startup Object , select the Sub Main.

Hope will help.
Avatar of nahumd
nahumd

Put a timer control on the form, change the interval to 1, and add the code:

Private Sub Timer1_Timer()
    Static i As Long
    i = i + 1
    Debug.Print i
End Sub

This is a better way to perform background tasks like you are trying to do.
The problem is that VB will take control COMPLETELY when the menu is focuses/clicked. No other code will run, even the timers...

The only workaround i know is that you hook the windows routine, but this makes your code "unstable" if you don't write your code properly...

Other option might be to develop an ActiveX exe which runs apart from your main application...

CHeers
I tested my code with timers, and it keeps on working in case of just focus on a simple menu.
It does not work in case of another modal form is opened.
timer will do it
ASKER CERTIFIED SOLUTION
Avatar of wileecoy
wileecoy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
VB timer have a strange behavior - when working in IDE, it stops when modal form/menu/msgbox appear, but works fine  when compiled.

Cheers
Hi Jasminshah,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept wileecoy's comment(s) as an answer.

Jasminshah, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
Per recommendation, force-accepted.

Netminder
CS Moderator