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?
Mike TomlinsonConnect With a Mentor Middle 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
    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
    End Sub

End Class

Open in new window

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.
Chris MillardAuthor Commented:
Could you post a small example of how I would do that please?
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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?
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
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.
Chris MillardAuthor Commented:
Sorry - but could I ask again for a small code sample?
Chris MillardAuthor Commented:
Works like a charm!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.