Stop the execute of command if it execute more than 3 seconds

Is it possible to stop the execute of a command if it executes more than 3 seconds?
If the command executes successfully, prompt a "Command execute success"
If the command executes failed, prompt a "Command execute failed"?

eg
Private Sub Command1_Click()
Call NonStopLoop
'if it executes more than 3 seconds, stop it. This is just an example, and obviously it won't stop within 3 seconds.
End Sub

Private Sub NonStopLoop()
Dim i As Integer
Do While True
    i = 1
Loop
End Sub
LVL 9
william007Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PreachDotNetCommented:
Put a timer in and set a module level variable called timeout.

Dim Timeout as boolean = false


Sub Command1_Click
Dim t As New System.Timers.Timer(3000)
AddHandler t.Elapsed, AddressOf TimerFired
Call NonStopLoop
End Sub


Private Sub NonStopLoop()
Dim i As Integer
Do While True And Not(Timeout)
    i = 1
Loop
End Sub


Public Sub TimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
   TimeOut = True
End Sub




PreachDotNetCommented:
Sorry Theres the return true or false bit

Private Function NonStopLoop() as boolean
Dim i As Integer
Do While True And Not(Timeout)
    i = 1
Loop
Return Timeout
End Sub
william007Author Commented:
Thanks preachDotNet,

Can we assume that the NonStopLoop is a API function, and we can't change the code of it,
is it possible to interfere to command execution just in Command1_Click?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

PreachDotNetCommented:
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
If you are working in VB6 then the answer is No.  You would need to put the code in Command1_Click() into a different thread which is difficult to do in VB6.  If the call you are making is a blocking call, then a Timer approach would not work because it won't fire until the blocking call returns.

If you are working in VB.Net then it is easy to make another thread and cancel it after a timeout period.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
william007Author Commented:
Thanks, this is a general question,
I have post my real problem here,
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21785775.html
Since if the remote server doesn't exist, the problem line will run forever, I wish to do something to let it time out for 3 seconds,
but now seems like I need to change my strategy.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.