Solved

VB Timers for forms and subs

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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