Solved

VB Timers for forms and subs

Posted on 2014-02-12
3
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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