473,545 Members | 666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use static variable to exit function?

I have a function with a number of long loops. While the function is
running, I want to be able to click a Stop button and exit the function as
quickly as possible.

The abbreviated code looks like this:

[code in form frm1]
Private Sub cmdStart_Click
Call bas1.LongLoops( parameter1, parameter2, parameterN)
End Sub

[code in standard module bas1]
Public Function LongLoops(param eter1, parameter2, parameterN)
Do While i < 10000
[code omitted]
Loop
Do While i < 20000
[code omitted]
Loop
'etc, etc...
End Function

Should I use a static variable?

[code in form frm1]
Private Sub cmdStop_Click
blnStop = True
Call bas1.LongLoops( blnStop, parameter2, parameterN)
End Sub

[code in standard module bas1]
Public Function LongLoops(blnSt op, parameter2, parameterN)
Static blnExit as Boolean
blnExit = blnStop
Do While i < 10000
If blnExit Then Exit Do
[code omitted]
Loop
Do While i < 20000
If blnExit Then Exit Do
[code omitted]
Loop
'etc, etc...
End Function

Or should I just raise an error that causes the function to exit?

[code in form frm1]
Private Sub cmdStop_Click
blnStop = True
Call bas1.LongLoops( blnStop, parameter2, parameterN)
End Sub

[code in standard module bas1]
Public Function LongLoops(blnSt op, parameter2, parameterN)
If blnStop Then Err.Raise MYCUSTOMERROR
Do While i < 10000
[code omitted]
Loop
Do While i < 20000
[code omitted]
Loop
'etc, etc...
End Function

If I raise an error with a second call to the function, will the process
from the first call continue running?

Other options?

Thanks in advance.
Nov 13 '05 #1
7 3115
On Sat, 3 Sep 2005 16:08:49 -0700, "deko" <de**@nospam.co m> wrote:

A quick-and-dirty solution would set a global variable to signal to
LongLoops to stop. If the code in bas1 is ONLY called from frm1, it
should be moved to the form, and a module-level global could be used.

A more elegant solution would perhaps test a public property in frm1
to check if the Stop button was clicked.

Don't forget to put a DoEvents in the LongLoops code, so the cmdStop
click would happen while it is executing. If you were using a "tight"
loop, it would first execute and THEN the click would occur - a little
too late.

I think raising an error is the wrong thing to do. It's abusing an
error handler feature for something it was not designed for.

-Tom.
I have a function with a number of long loops. While the function is
running, I want to be able to click a Stop button and exit the function as
quickly as possible.

The abbreviated code looks like this:

[code in form frm1]
Private Sub cmdStart_Click
Call bas1.LongLoops( parameter1, parameter2, parameterN)
End Sub

[code in standard module bas1]
Public Function LongLoops(param eter1, parameter2, parameterN)
Do While i < 10000
[code omitted]
Loop
Do While i < 20000
[code omitted]
Loop
'etc, etc...
End Function

Should I use a static variable?

[code in form frm1]
Private Sub cmdStop_Click
blnStop = True
Call bas1.LongLoops( blnStop, parameter2, parameterN)
End Sub

[code in standard module bas1]
Public Function LongLoops(blnSt op, parameter2, parameterN)
Static blnExit as Boolean
blnExit = blnStop
Do While i < 10000
If blnExit Then Exit Do
[code omitted]
Loop
Do While i < 20000
If blnExit Then Exit Do
[code omitted]
Loop
'etc, etc...
End Function

Or should I just raise an error that causes the function to exit?

[code in form frm1]
Private Sub cmdStop_Click
blnStop = True
Call bas1.LongLoops( blnStop, parameter2, parameterN)
End Sub

[code in standard module bas1]
Public Function LongLoops(blnSt op, parameter2, parameterN)
If blnStop Then Err.Raise MYCUSTOMERROR
Do While i < 10000
[code omitted]
Loop
Do While i < 20000
[code omitted]
Loop
'etc, etc...
End Function

If I raise an error with a second call to the function, will the process
from the first call continue running?

Other options?

Thanks in advance.


Nov 13 '05 #2
> A quick-and-dirty solution would set a global variable to signal to
LongLoops to stop.
sounds interesting.
If the code in bas1 is ONLY called from frm1, it
should be moved to the form, and a module-level global could be used.
It is, but there's so much code it needs to be segregated by purpose.
A more elegant solution would perhaps test a public property in frm1
to check if the Stop button was clicked.

Don't forget to put a DoEvents in the LongLoops code, so the cmdStop
click would happen while it is executing. If you were using a "tight"
loop, it would first execute and THEN the click would occur - a little
too late.
10-4
I think raising an error is the wrong thing to do. It's abusing an
error handler feature for something it was not designed for.


perhaps...

Here's what I've got working:

[code in form frm1]
Private Sub cmdStart_Click
Call bas1.LongLoops( False, parameter2, parameterN)
End Sub
Private Sub cmdStop_Click
Call bas1.LongLoops( True)
End Sub

[code in standard module bas1]
Public Function LongLoops(blnSt op As Boolean, Optional parameter2, Optional
parameterN)
Static blnExit As Boolean
blnExit = blnStop
Do While i < 10000
If blnExit Then Exit Do
DoEvents
[code omitted]
Loop
Do While i < 20000
If blnExit Then Exit Do
DoEvents
[code omitted]
Loop
[code omited]
End Function

The problem (what I thought might be avoided by raising an error) is that I
have to instantiate a bunch of objects to get into the loop (on the second
call made from cmdStop).

This does not work:

[code in standard module bas1]
Public Function LongLoops(blnSt op As Boolean, Optional parameter2, Optional
parameterN)
Static blnExit As Boolean
blnExit = blnStop

If Not blnStop Then

Set obj1 = Object1
Set obj2 = Object2
Set objN = Object N
Do While obj1 < obj2
If blnExit Then Exit Do
DoEvents
Test objN
[code omitted]
Loop

End If

End Function

The reason this does not work is because (as this new example indicates) the
actual code uses objects rather than integers in the looping construct.

So the objects have to be set to get into the loop. So the second call
needs to get into the loop or blnExit does not change - which I find strange
because I thought I didn't need to get into the loop since blnExit is
Static. That is, I thought just getting into the function with the second
call and changing the value of the Static variable (without getting into the
loop) would cause the Static variable to change regardless of where it is
being used.

Perhaps this would be the case with a global variable? That would be better
because I wouldn't have to instantiate all those objects (just to get into
the loop so I can exit it). But perhaps I could use some Resume Next
statements If blnStop = True or something...

Anyway, thanks for the reply.
Nov 13 '05 #3
The last time I played with this type of thing, I finished up having to
"break-out" of the executing function. I did this in rather elegant style
by throwing a message box every 20 loops asking if I wanted to continue or
not. I'm just wondering if a refinement on that is to breakout to a
function that tests the public variable.

if TestLoop = 20 then
If BreakOut() Then Exit Do
...

Function Breakout() as boolean
BreakOut = PublicStop
End Function

Then from your form ....
Private Sub cmdStop_Click
PublicStop = True
Endsub

The only thing I'm not sure of is whether the button on the form can alter
the public while your LongLoops is executing. If not, well, elegance is
worth pursuing!

--
Regards,
Kevin

"deko" <de**@nospam.co m> wrote in message
news:Q8******** *************** *************** ***@comcast.com ...
A quick-and-dirty solution would set a global variable to signal to
LongLoops to stop.
sounds interesting.
If the code in bas1 is ONLY called from frm1, it
should be moved to the form, and a module-level global could be used.
It is, but there's so much code it needs to be segregated by purpose.
A more elegant solution would perhaps test a public property in frm1
to check if the Stop button was clicked.

Don't forget to put a DoEvents in the LongLoops code, so the cmdStop
click would happen while it is executing. If you were using a "tight"
loop, it would first execute and THEN the click would occur - a little
too late.
10-4
I think raising an error is the wrong thing to do. It's abusing an
error handler feature for something it was not designed for.


perhaps...

Here's what I've got working:

[code in form frm1]
Private Sub cmdStart_Click
Call bas1.LongLoops( False, parameter2, parameterN)
End Sub
Private Sub cmdStop_Click
Call bas1.LongLoops( True)
End Sub

[code in standard module bas1]
Public Function LongLoops(blnSt op As Boolean, Optional parameter2, Optional
parameterN)
Static blnExit As Boolean
blnExit = blnStop
Do While i < 10000
If blnExit Then Exit Do
DoEvents
[code omitted]
Loop
Do While i < 20000
If blnExit Then Exit Do
DoEvents
[code omitted]
Loop
[code omited]
End Function

The problem (what I thought might be avoided by raising an error) is that I
have to instantiate a bunch of objects to get into the loop (on the second
call made from cmdStop).

This does not work:

[code in standard module bas1]
Public Function LongLoops(blnSt op As Boolean, Optional parameter2, Optional
parameterN)
Static blnExit As Boolean
blnExit = blnStop

If Not blnStop Then

Set obj1 = Object1
Set obj2 = Object2
Set objN = Object N
Do While obj1 < obj2
If blnExit Then Exit Do
DoEvents
Test objN
[code omitted]
Loop

End If

End Function

The reason this does not work is because (as this new example indicates) the
actual code uses objects rather than integers in the looping construct.

So the objects have to be set to get into the loop. So the second call
needs to get into the loop or blnExit does not change - which I find strange
because I thought I didn't need to get into the loop since blnExit is
Static. That is, I thought just getting into the function with the second
call and changing the value of the Static variable (without getting into the
loop) would cause the Static variable to change regardless of where it is
being used.

Perhaps this would be the case with a global variable? That would be better
because I wouldn't have to instantiate all those objects (just to get into
the loop so I can exit it). But perhaps I could use some Resume Next
statements If blnStop = True or something...

Anyway, thanks for the reply.

Nov 13 '05 #4
rkc
deko wrote:
I have a function with a number of long loops. While the function is
running, I want to be able to click a Stop button and exit the function as
quickly as possible. Other options?


Wrap the whole shebang in a class. Everything is in one place, shielded
from outside influence and re-usable.

'<clsDekoLoop>
Option Compare Database
Option Explicit

Private WithEvents btnStop As Access.CommandB utton
Private exitLoop As Boolean
Private mElapsedTime As String

Public Property Get ElapsedTime() As String
ElapsedTime = mElapsedTime
End Property

Public Sub Init(btn As Access.CommandB utton)
Set btnStop = btn
btnStop.OnClick = "[Event Procedure]"

End Sub

Private Sub btnStop_Click()
exitLoop = True
End Sub

Public Sub RunLoop(outerLo op As Long, innerLoop As Long)
Dim x As Long
Dim y As Long
Dim b As Single
b = Timer

For x = 1 To outerLoop
DoEvents
For y = 1 To innerLoop: Next
If exitLoop Then Exit For
Next
mElapsedTime = "Outer Loop: " & x & vbCrLf & _
"Elapsed time: " & Timer - b

End Sub

Private Sub Class_Terminate ()
If Not btnStop Is Nothing Then Set btnStop = Nothing
End Sub

'</clsDekoLoop>

'<frmTestDekoLo op>

CommandButton: cmdStart
Command Button: cmdStop

Private Sub cmdStart_Click( )
Call TestDekoLoop
End Sub

Sub TestDekoLoop()
Dim dl As clsDekoLoop
Set dl = New clsDekoLoop

dl.Init Me.cmdStop

dl.RunLoop 10000, 1000

MsgBox dl.ElapsedTime

Set dl = Nothing

End Sub

'</frmTestDekoLoop >
Nov 13 '05 #5
> Wrap the whole shebang in a class. Everything is in one place, shielded
from outside influence and re-usable.


I was exploring that idea yesterday, but got things working... other fish to
fry now.

Any error generated by LongLoops is passed back to cmdStop_Click. So with
an 'OnError Resume Next' statement in the calling sub, the program flow
continues into the loop (with blnExit set to True) and the loops exit.

Private Sub cmdStop_Click
OnError Resume Next 'mainly concerned with Error 91
Call bas1.LongLoops( True)
End Sub

Public Function LongLoops(blnSt op As Boolean, Optional parameter2, Optional
parameterN)

Static blnExit As Boolean
blnExit = blnStop
Set obj1 = Object1
Set obj2 = Object2
Set objN = Object N
Do While obj1 < obj2
If blnExit Then Exit Do
DoEvents
Test objN
[code omitted]
Loop

End Function

I'm still wondering why I have to enter the loop with the second call - I
thought a Static variable would change value everywhere in the function once
it's passed to the function.
Nov 13 '05 #6
rkc
deko wrote:
So the objects have to be set to get into the loop. So the second call
needs to get into the loop or blnExit does not change - which I find strange
because I thought I didn't need to get into the loop since blnExit is
Static. That is, I thought just getting into the function with the second
call and changing the value of the Static variable (without getting into the
loop) would cause the Static variable to change regardless of where it is
being used.

Perhaps this would be the case with a global variable? That would be better
because I wouldn't have to instantiate all those objects (just to get into
the loop so I can exit it). But perhaps I could use some Resume Next
statements If blnStop = True or something...


I think the problem is that when you execute the procedure again while
the first call is still running the second call has to run to completion
before the first call can resume running. Frankly I am surprised the
whole thing doesn't just blow up in your face. The second thing that
isn't clear is what does it mean for one object to be less than another
object. One object is equal to another object when they both reference
the same instance.

Any way if you intent on doing this your way, try checking blnExit
immediatley after assigning it and exiting the function right then
if true.

blnExit = blnStop
If blnExit Then Exit Function
Nov 13 '05 #7
> blnExit = blnStop
If blnExit Then Exit Function


I need the loops to complete because they call other functions (there are a
few, and I don't know which one the code will be in when the user clicks the
Stop button) - then the normal flow of the procedure will continue to the
end.

It's working fine. My only confusion was dealing with the Static Boolean
variable.

As for the objects being greater or less than each other, that example was
just to keep things concise. The point is the loops require objects (one of
which is a collection) to be instantiated.
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3918
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /* in above case where is variable k and j mapped in memory layout ? */
3
2792
by: XPhaktor | last post by:
In C#, how do I replace using VB local static variable declarations to handle method reentrancy. Note, if you use a class-scope variable instead of a local one, then you run the risk of inadvertently changing the variable's value in another method that is performing the same technique. The Interlocked.Exchange class uses a class-scope class...
18
4028
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
9
8626
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the...
55
6165
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
10
2634
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
8
2443
by: jayaramganapathy | last post by:
Hello friends, I have a map like std::map< std::string , std::map<std::string, std::string>* EpPropCache::propertyCache ; (This is a static instance and taken from *.cpp file) As you can see the value is a pointer to another map. I do new of std::map<std::string, std::stringand add the pointer to the map. Do I have to delete the map...
8
5695
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
4
2203
by: HxRLxY | last post by:
I am having a compile-time problem with a simple program I am writing. When I attempt to compile, I get the error "non-static variable this cannot be referenced from a static context". The error point to where I try to create a new instance of 'ConvertToOunces' inside the main method. Any help would be greatly appreciated. Here is my source...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7419
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1879
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.