Click here to Skip to main content
Click here to Skip to main content
Go to top

Background Cycle Timing

, 20 Aug 2014
Rate this:
Please Sign up or sign in to vote.
High Resolution Timer EventArg Class for timing background tasks.


I so often have to monitor what's happening in a background process and got tired of re-writing the code for this in so many ways. So I figured I'd share what I have with you, especially in the light of the fact that I've gotten so many helpful code snippets from you all. This is the first article I've submitted and have had a little trouble with the source formateeing Wink | ;) . The source itself is formatted fine.


Basically, the High Resolution Timing function is from Microsoft at:

That example was for Windows CE, so I changed the DllIMports to "kernel32.dll" instead of "coredll.dll" as in their example, and added various cycle timing/display methods.

Then I Inherited it into a very basic EventArg class such as is used with the BackgroundWorker class. So the timing details of my background processes would always be easily available as these args get passed around from EventHandler to EventHandler with slight variations.

Using the code

Except for the HiResTimer class, I've tried to keep it as simple as possible. The EventArg class that inherits it is very basic, but you could add what you want to it for specific purposes. The idea behind the HiResTimer class was to write it once and have high resolution timing details easily available and somewhat transparent to the actual work being done.

Outside of using the HiResTimer as a base for an EventArgs class, using the class in a stand-alone way is illustrated below:

// add the ClassHighResTimer.cs file to your project
// add a StatusStrip with a label and a progress bar
// and a Button to your Form
// add these to your using statements
// using AbsoluteGenius;
// using System.Threading;
// then put this in as your button1_Click event code
private void button1_Click(object sender, EventArgs e)
    // this creates and inits cycle timing
    Utility.HiResTimer hiResTimer = new Utility.HiResTimer();
    Int64 totalCount = 1000; // loop counter total count
    // if there's a lot of processing before start
    // hiResTimer.StartTime = DateTime.Now; // (optional) will re-init cycle timing
    while (hiResTimer.CycleCount != totalCount - 1)
        // do whatever work intended here
        hiResTimer.UpdateCycleStats(); // updates cycle stats and hiResTimer.CycleCount
        toolStripStatusLabel1.Text = " Run Time: " + hiResTimer.RunningTime 
                                   + " Avg Time: " + hiResTimer.CycleAvgTime
                                   + " Cycle Time: " + hiResTimer.CycleTime;
        toolStripProgressBar1.Value = hiResTimer.CyclePercentage(totalCount);
    // do whatever post-loop processing (display/save data, etc.)
    // hiResTimer.EndTime = DateTime.Now; // (optional) logs completion of overall task
    toolStripStatusLabel1.Text = " Run Time: " + hiResTimer.RunningTime // the total while loop time
                               + " Avg Time: " + hiResTimer.CycleAvgTime// average while loop interval
                               + " Total Time: " + hiResTimer.TotalTime;// total task time 
                                                                        // (also logs completion and EndTime)
    // and that's all there is to it

The EventArgs class simply inherits this same functionality:

public class HiResEventArgs : HiResTimer
    #region "Members"
    private object argument = null;
    public object RealResult = null;
    public object RealUserState = null;

    #region "Construction"
    public HiResEventArgs(object argument)
        : base() // base() inits the cycle timing
        this.argument = argument; // pick up the argument
    public static HiResEventArgs CopyOfHiResEventArgs(HiResEventArgs hiResEventArgs)
        HiResEventArgs returnCopy = new HiResEventArgs(hiResEventArgs.argument);
        returnCopy.RealUserState = hiResEventArgs.RealUserState;
        returnCopy.RealResult = hiResEventArgs.RealResult;
        return returnCopy;

    #region "Properties"
    public object Argument
        get { return argument; }

So, for example, the above stand-alone code moved over to a DoWork function of a BackgroundWorker class would be very similar, except it would be using a HiResEventArgs variable as the argument sent to it. The button1_Click handler would create the HiResEventArgs object and send it to the DoWork function as the arguument/paramater to RunWorkerAsync.

Utility.HiResEventArgs hiResArgs = new Utility.HiResEventArgs(100); // 100 loop total as argument

The DoWork function would pick this up and use it pretty much the same as the stand-alone example, except it wouldn't display anything:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    BackgroundWorker bw = sender as BackgroundWorker;
    Utility.HiResEventArgs hiResArgs = e.Argument as Utility.HiResEventArgs;
    // pick up the argument
    Int64 totalCount = 0; // loop counter total count
    Int64.TryParse((hiResArgs.Argument != null ? hiResArgs.Argument.ToString() : "100"), out totalCount);
    // if there's a lot of processing before start
    // hiResArgs.StartTime = DateTime.Now; // (optional) will re-init cycle timing
    while (hiResArgs.CycleCount < totalCount && !bw.CancellationPending)
        // do whatever work intended here
        hiResArgs.UpdateCycleStats(); // updates cycle stats and cycle counter
        hiResArgs.RealUserState = null; // (optional) fill this up with whatever state data you want 
        hiResArgs.RealResult = null; // (optional) fill this up with whatever result data you want 
        // send the hiResArgs to the ProgressChanged code as e.UserState
        bw.ReportProgress(hiResArgs.CyclePercentage(totalCount), // (important) sets total cycles 
        Utility.HiResEventArgs.CopyOfHiResEventArgs(hiResArgs)); // send a copy as e.UserState 
    // send the hiResArgs to the RunWorkerCompleted code as e.Result
    e.Result = Utility.HiResEventArgs.CopyOfHiResEventArgs(hiResArgs); // send a copy as e.Result
    // finally make sure to flag Cancel if necessary
    if (bw.CancellationPending)
        e.Cancel = true;

The ProgressChanged function takes over the display of the progress that was previously

handled directly in the stand-alone example:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    if (((BackgroundWorker)sender).CancellationPending)
    if (e.UserState != null && e.UserState.GetType().ToString().EndsWith("HiResEventArgs"))
        Utility.HiResEventArgs hiResArgs = e.UserState as Utility.HiResEventArgs;
        toolStripStatusLabel1.Text = hiResArgs.CyclePercentage().ToString() + "% Done... "
                                   + " Run Time: " + hiResArgs.RunningTime
                                   + " Avg Time: " + hiResArgs.CycleAvgTime
                                   + " Cycle Time: " + hiResArgs.CycleTime;
        toolStripProgressBar1.Value = hiResArgs.CyclePercentage();
        toolStripProgressBar1.Value = e.ProgressPercentage;

And the final results with TotalTime would be handed in the RunWorkerCompleted Event Handler:

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    if (e.Cancelled == false && e.Error == null
    && e.Result != null && e.Result.GetType().ToString().EndsWith("HiResEventArgs"))
        Utility.HiResEventArgs hiResArgs = e.Result as Utility.HiResEventArgs;
        // do whatever post-loop processing (display/save data, etc.)
        // hiResArgs.EndTime = DateTime.Now; // (optional) logs completion of overall task
        toolStripStatusLabel1.Text = hiResArgs.CyclePercentage().ToString() + "% Completed " 
                                   + " Run Time: " + hiResArgs.RunningTime  // the total while loop time
                                   + " Avg Time: " + hiResArgs.CycleAvgTime // average while loop interval
                                   + " Total Time: " + hiResArgs.TotalTime; // total task time 
                                                                            // (also logs completion and EndTime)
    button1.Text = "Start";


Sean O'Leary



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


About the Author

Software Developer (Senior) Absolute Genius
Canada Canada
Design and Develop Custom Electronics and Software to control them.
Software Engineering of custom Business Software System.
Hardware Engineering of Custom Electronics Control devices.

Comments and Discussions

QuestionDownload don't work Pinmemberdescartes17-Aug-14 23:21 
AnswerRe: Download don't work Pinmembersgoleary18-Aug-14 3:22 
GeneralRe: Download don't work Pinmemberdescartes18-Aug-14 3:35 
GeneralRe: Download don't work Pinprofessionalwallkao3bei418-Aug-14 8:20 
GeneralRe: Download don't work Pinmembersgoleary18-Aug-14 16:25 
GeneralRe: Download don't work Pinprofessionalwallkao3bei418-Aug-14 18:01 
SuggestionDon't test the textual name of a class PinmemberJohn Brett11-Aug-14 5:38 
GeneralRe: Don't test the textual name of a class Pinmembersgoleary15-Aug-14 14:19 
GeneralRe: Don't test the textual name of a class PinmemberPIEBALDconsult18-Aug-14 5:15 
GeneralRe: Don't test the textual name of a class Pinmembersgoleary23-Aug-14 6:56 
Questionthis vs System.Diagnostics.Stopwatch PinmemberSpirch11-Aug-14 2:21 
AnswerRe: this vs System.Diagnostics.Stopwatch Pinmembersgoleary14-Aug-14 18:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 20 Aug 2014
Article Copyright 2014 by sgoleary
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid