Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6990013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:17:24+00:00 2026-05-27T19:17:24+00:00

What should be straight forward is not here and I couldnt find a way

  • 0

What should be straight forward is not here and I couldnt find a way yet in spite of reading a lot.

I have a button which executes a time consuming function. So on clicking the button should show time elapsed in milliseconds in a label with an interval of 500 ms. And when the desired result is achieved I want the timer to stop. I dont just need the final time (the total time consumed) in a label, but the label should dynamically show the time being elapsed. My code would be:

    private void btnHistory_Click(object sender, EventArgs e)
    {
        Class1 c = new Class1();
        c.StartClock(ref label12);

        Utility.PopulateHistory(dgvRecords_history, _util); //time consuming function

        c.StopClock();
    }

And in Class1 I write this:

    internal void StartClock(ref Label l)
    {
        Timer t = new Timer();
        t.Interval = 500;
        t.Enabled = true;
        t.Tag = l;
        t.Tick += new EventHandler(t_Tick);
        t.Start();
    }

    int i;
    bool stop;
    void t_Tick(object sender, EventArgs e)
    {
        if (stop)
        {
            ((Timer)sender).Stop();
            return;
        }

        ((Label)((Timer)sender).Tag).Text = (++i).ToString();
    }

    internal void StopClock()
    {
        i = 0;
        stop = true;
    }

What happens is, the t_Tick event is fired only after the complete code under button event is fired. That is the tick event is fired after it goes through the StopClock function! I got no idea why on earth it should be that!

2 questions basically:

  1. How can my requirement be achieved in the right way to handle these? I know I should use other built in classes to evaluate performance, but this is just for display purpose. For this, what is the ideal approach?

  2. Why is my code not working?

EDIT: I have used here System.Windows.Forms Timer here, but the result is not any different with System.Timers Timer

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:17:25+00:00Added an answer on May 27, 2026 at 7:17 pm

    The problem is that your long-running task is also running on the UI thread. So the timer can’t fire and update the UI, since the thread is busy handling the long-running task.

    Instead, you should use a BackgroundWorker to handle the long-running task.

    In code:

    private void btnHistory_Click(object sender, EventArgs e) 
    { 
        Class1 c = new Class1(ref label12); 
        c.StartClock(); 
    
        var backgroundWorker = new BackgroundWorker();
        backgroundWorker.DoWork += (s, e) =>
            {
                // time consuming function
                Utility.PopulateHistory(dgvRecords_history, _util);
            };
    
        backgroundWorker.RunWorkerCompleted += (s, e) =>
            {
                c.StopClock();
            };
    
        backgroundWorker.RunWorkerAsync();
    } 
    

    As ChrisWue noted, since you now have the long-running task in a separate thread, it needs to invoke any access to the UI controls on the UI thread.

    If your long-running task just needs some data from the UI to start, you can pass that data as parameter of RunWorkerAsync(). If you need to output some result data to the UI, you can do that in the handler of the RunWorkerCompleted event. If you occasionally need to update the UI as progress is being made, you can do that in the handler of the ProgressChanged event, calling ReportProgress() in your DoWork handler.

    If none of the above are needed, you could use the ThreadPool, as in StaWho’s answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be straight forward for a guru. I don't have any code really
This seems like it should be pretty straight forward, but I'm apparently confused. I
This might be something very straight forward and I really think it should work
I have a simple XML extraction issue that should be solvable with straight PHP
Pretty straight forward question, I have a GtkEntry widget that I want to set
Sitting here with a simple rails 3 app in which I have a simple
These questions are relatively straight forward. When using vectors, should I use the new
This should be pretty straightforward I would think. I have this string: [quote=Joe Johnson|1]Hi![/quote]
This is quite straight forward. I'm calling a web-service (.asmx, with session enabled) from
I have a supervisor which starts simple_one_for_one children. Each child is in fact a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.