Solved

VB Timers for forms and subs

Posted on 2014-02-12
3
180 Views
Last Modified: 2014-02-27
I'm working on a VB forms project (using VB 2010 Express) that currently has a number of database or API interrogation subroutines.  These subs can each take several minutes to run.

Form name = Form1
There's a single label for overall elapsed time lblElapsed
Sub names = Sub1, Sub2, etc...
The subs are started by button_click events - next to each button is a label: lblSub1, lblSub2, etc.

While the application is running, I'd like to display the overall elapsed time since Form1_Load, in the label (lblElapsed) updated each second.  I'd also like the whole form redrawn (if that's the right word for it!) every second or two  Otherwise, when I tab between other programs and back to it, the form is only partially rendered.
 
While each sub is running, I'd like to display a label (lblSub1, lblSub2, etc.) with the word "running -" and the elapsed time, updated each second.
When each sub completes, I'd like the associated label to display "completed - " and the elapsed time for that sub to complete.

I'm floundering somewhat, having seen a load of examples of system.threading.timers which don't quite explain how they work, or can be made to work in my scenario.  I've got a standard timer to work but it stops updating as soon as any sub is active.

A basic example of code to achieve this would really help.
0
Comment
Question by:Beamson
3 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 150 total points
ID: 39853716
>>I've got a standard timer to work but it stops updating as soon as any sub is active.

As expected, your sub is using resources in the main thread which means tasks such as updating the interface are blocked until the sub finishes.  What you have to do is delegate the long running tasks into another thread, eg. a background worker.
One example is here:
http://stackoverflow.com/questions/6376972/how-to-dynamically-create-a-background-worker-in-vb-net

You can easily find other examples
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 100 total points
ID: 39854327
For the Screen updating, no need to refresh it regularly, simply react to the Paint event. It is triggered when you come back from another application.

For the long running routines, check the followings in online help, they help you run methods asynchronously, so that your application can continue working and a simple Form timer should be sufficient for your screen display:

BackgroundWorker control is the easiest way to work with threads in a Form.

If you are running your database routine through Command objects, lookup the BeginExecute... methods, that run the queries in the background while your application continues working.

If you are on Visual Studio 2012 or 2013, the new Async keyword also enables you to launch long running methods so that they run in the background.
0
 
LVL 1

Author Closing Comment

by:Beamson
ID: 39891294
It took a while to put it to the test and modify the examples to fit my scenario - in the end it did what I needed.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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