VB 2012 - Run code for a specific length of time

Using VB 2012, how can I set my subroutine to run for a specific length of time? I want a routine to run for 10 seconds only then stop.
LVL 17
Chris MillardAsked:
Who is Participating?
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.

Deepu SreedharBI Software EngineerCommented:
Put a timer with an Interval of 10 sec.
Enable the timer with the routine start
On the first tick of the timer, stop the routine and disable the timer.
0
Chris MillardAuthor Commented:
Could you post a small example of how I would do that please?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
There are always caveats to this.  What kind of project?  Console, WinForms, WebForms, WPF, etc...

What are you going to be doing for ten seconds?  Do you need to update the GUI during this period?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Chris MillardAuthor Commented:
It's a just for fun Win Form that I am doing. During the 10 seconds, my program is going to perform some mathematical calculations and put the results to a textbox
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Gotcha.  Since this is WinForms, you need to put those mathematical calculations in a background thread, otherwise the GUI will lock up for those ten seconds.  The BackgroundWorker() control would be perfect.

In the DoWork() handler you can put your code to do the calculations.  You can put that code in a loop that checks the time to make it run for ten seconds.  Use either the ProgressChanged() or RunWorkerCompleted() events to update the GUI.
0
Chris MillardAuthor Commented:
Sorry - but could I ask again for a small code sample?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sure...add a BackgroundWorker() control to your Form:
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim PI As Double = 3.0

        ' ... do some kind of calculation for ten seconds ...
        Dim dt As DateTime = DateTime.Now.AddSeconds(10)
        While dt > DateTime.Now

            PI = PI + Math.Sin(PI)

            ' do NOT update the GUI from in here!
            ' if you want progress updates use BackgroundWorker1.ReportProgress() and the ProgressChanged() event!

        End While

        ' pass the result to the RunWorkerCompleted() event below:
        e.Result = PI
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        ' grab the result:
        Dim PI As Double = e.Result

        ' It is SAFE to update the GUI from within this event:
        TextBox1.Text = PI
        Button1.Enabled = True
        MessageBox.Show("Done!")
    End Sub

End Class

Open in new window

0

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
Chris MillardAuthor Commented:
Works like a charm!
0
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.NET

From novice to tech pro — start learning today.