Solved

VB Timers for forms and subs

Posted on 2014-02-12
3
177 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

9 Experts available now in Live!

Get 1:1 Help Now