We help IT Professionals succeed at work.

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

Jasminshah
Jasminshah asked
on
Medium Priority
235 Views
Last Modified: 2010-05-02
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...




 
Comment
Watch Question

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
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.

Commented:
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.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
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

Commented:
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.

Commented:
timer will do it
Commented:
There are examples out there for multi-threading in VB.

Try PlanetSourceCode.  Here is a link to a search that will give you some examples and tutorials.

http://www.planetsourcecode.com/vb/scripts/BrowseCategoryOrSearchResults.asp?txtCriteria=multi+thread&blnWorldDropDownUsed=TRUE&txtMaxNumberOfEntriesPerPage=10&blnResetAllVariables=TRUE&lngWId=1&B1=Quick+Search&optSort=Alphabetical

Good Luck.

Wileecoy.
Ark
CERTIFIED EXPERT

Commented:
VB timer have a strange behavior - when working in IDE, it stops when modal form/menu/msgbox appear, but works fine  when compiled.

Cheers
CERTIFIED EXPERT
Author of the Year 2009

Commented:
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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.