Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stopping Background Processes -> E-Stop Button 1

Status
Not open for further replies.

jsaliers

Programmer
Jan 23, 2003
129
0
0
US
I have an application that runs in cycles, and when running will generally run for hours at a time. I need a way for the user to stop the application, but keep the form loaded. I need it to end, not pause. I found the thread below, and was thinking I could use something similar to this.

thread222-63969

However, I need to know if I can call the message box API, and instead of being an "OK" box, be a "E-STOP" box, that will stop the application. One important thing to note: The application is setting and de-setting a pin on the com port. This pin must be set to low, before the application stops.

Any help would be much appreciated. Thanks in advance.

Jon
 
I thought of something last night. Would it work to load a second nearly identical form with a button on it, that would stop the processes of the first form when the button was pressed? The second form could then be closed, leaving the original form, but the application would be stopped.

Any ideas?

Jon
 
Could you not use a module-level flag, that when set causes the routine in question to exit and reset flag?
Code:
Private mblnFlag As Boolean

Private Sub DoSomething
  Do
    DoStuff
  Loop Until mblnFlag
  mblnFlag = vbFalse
End Sub

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"A computer program does what you tell it to do, not what you want it to do." -- Greer's Third Law
 
Sounds good to me, but I have a few questions about it for you. I would put the main code inside this Do Loop. It would then stop running when the flag is set. How can I get the flag set when I press a button if the code is running? I am not familiar with flags in VB.

Thank you.

Jon
 
If you include a DoEvents just before the Loop Until then if you have clicked on the "Stop Damn You!" button it'll allow the Command_Click event code to be processed:
Code:
Private Sub cmdStopDammit_Click()
  mblnFlag = vbTrue
End Sub

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"A computer program does what you tell it to do, not what you want it to do." -- Greer's Third Law
 
Just to clarify, my loop would look something like this:

Code:
Private mblnFlag As Boolean

Private Sub DoSomething
  Do
    DoStuff
    DoEvents
  Loop Until mblnFlag
  mblnFlag = vbFalse
End Sub

Private Sub cmdStopDammit_Click()
  mblnFlag = vbTrue
End Sub

So now, each time through the loop, it will look for an event. This leads to another problem. In my loop, I am looping and waiting for something between 1 second, and 285 minutes. The problem is, if I want to stop it in the middle of the 285-minute loop, I need to. However, if i put it in the 1 second loop, I need the precision to be relatively precise. Here is what I have:

Code:
Do While DateDiff(&quot;s&quot;, startTime, Now()) < stage1Duration

   'Nothing is actually being done, except waiting for time to pass

Loop
[\code]

There will be 10 of these loops in main loop.  If I put a DoEvents inside each loop, I need to make sure that if my duration is one second, it will break the loop after 1 second.  If the DoEvents action takes too long, I will lose my precision.

Any thoughts?

Jon
 
DoEvents merely gets VB to allow other processes a chance to execute - if you have a do .. loop without any opportunity for yeilding processing time for other processes then you will never be able to click a button, because the code is never given any time to run.
Code:
Do While DateDiff(&quot;s&quot;, startTime, Now()) < stage1Duration

  'Nothing is actually being done, _
   except waiting for time to pass

  DoEvents
  If mblnFlag Then Exit Do
Loop

Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;A computer program does what you tell it to do, not what you want it to do.&quot; -- Greer's Third Law
 
Thank you, you have been very helpful!

Jon
 
My pleasure. Thanks for the star!

Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;A computer program does what you tell it to do, not what you want it to do.&quot; -- Greer's Third Law
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top