?
Solved

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

Posted on 2006-03-23
6
Medium Priority
?
216 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:william007
  • 3
  • 2
6 Comments
 
LVL 6

Assisted Solution

by:PreachDotNet
PreachDotNet earned 800 total points
ID: 16267866
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




0
 
LVL 6

Expert Comment

by:PreachDotNet
ID: 16267876
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
0
 
LVL 9

Author Comment

by:william007
ID: 16267931
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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Assisted Solution

by:PreachDotNet
PreachDotNet earned 800 total points
ID: 16268028
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1200 total points
ID: 16269148
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.
0
 
LVL 9

Author Comment

by:william007
ID: 16269392
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.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question