Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display time elapsed on Label control on windows form. for that i am using System.Timers.timer but unable to update Label with the elapsed time on button click event.
tried Timer tick event of Timer but no luck, i want to update elapsed time on label control as soon as user click Process button till all the functions get executed on button click.
Please help.

code for reference :

VB
dim mdate as date
private sub btnprocess_click()
mdate=date.now

timer.enabled=true
timer.start()
timer.interval=100

'below functions take much time in processing
'Approx 20 to 30 minutes to complete all functionalities.

function1
function2
function3

timer.enabled=false

end sub

private sub timer_tick()
Dim ts As TimeSpan = DateTime.Now.Subtract(mDate)
lblelapsed.Text = ts.Hours  & ":" & ts.Minutes  & ":" & ts.Seconds 
Update()
end sub
Posted
Updated 9-Oct-12 19:47pm
v3
Comments
Vardhraz 9-Oct-12 2:17am    
use this
string str1=System.DateTime.Now.ToString("hh-mm-ss");

you can get system time...whenever user clicking button store that value as start time.while stop the process again use that System.DateTime.Now();now u can get stopped time..calculate difference you can get elapsed time..i hope this may helpful for you....

my background is C# you need to convert the code to VB.Net,
Try like this:
C#
DateTime mdate;
System.Timers.Timer tim;
bool isProcessing = true;

private void button1_Click(object sender, EventArgs e)
{
    mdate = DateTime.Now;
    tim = new System.Timers.Timer(1000);
    tim.Elapsed += new System.Timers.ElapsedEventHandler(tim_Elapsed);
    tim.Start();

    new Thread(new ThreadStart(doTheJob)).Start();

    while (isProcessing)
    {
        Thread.Sleep(100);
        Application.DoEvents();
    }
    tim.Stop();
}

private void doTheJob()
{
    //all your code like
    //function1
    //function2
    //function3
    isProcessing = false;
}

void tim_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    TimeSpan ts = DateTime.Now.Subtract(mdate);

    label1.Text = ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds + ":" + ts.Milliseconds;

    Application.DoEvents();
}
now we are using System.Timers.Timer instead of System.Forms.Timer.
 
Share this answer
 
v2
Comments
ixashish 9-Oct-12 2:10am    
Thank you Tanveer, added update to code but still it is not updating label, i am trying to show user elapsed time till the process get completed.

private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
timer1.Interval = 100;

// Some functions which take approx 15 to 30 minutes to execute.
}

private void timer1_Tick(object sender, EventArgs e)
{
Dim ts As TimeSpan = DateTime.Now.Subtract(Date.Now)
label1.Text = ts.Seconds
Update();//this will update the form and new result will be shown
}
tanweer 9-Oct-12 2:15am    
you need to put your
// Some functions which take approx 15 to 30 minutes to execute.
inside the timer1_Tick before the Update(), because the button click event is fired only once and the tick event of Timer will be executing after every 100 milli sec.
ixashish 9-Oct-12 3:31am    
Hello Tanveer ,
tried solution as sample program it works,but in my program functions are written on button click event and i want to execute function only once so is it reliable to put functions before update ?

My function has some queries which get data from database and show results to user.
tanweer 10-Oct-12 0:57am    
please go to improve question and put your code here so that we can help you out, thanks
ixashish 10-Oct-12 1:43am    
hi Tanweer ,Please check improved question with sample code
use this
string str1=System.DateTime.Now.ToString("hh-mm-ss");

you can get system time...whenever user clicking button store that value as start time.while stop the process again use that System.DateTime.Now();now u can get stopped time..calculate difference you can get elapsed time..i hope this may helpful for you....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900