Solved

VB Timers for forms and subs

Posted on 2014-02-12
3
178 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now