Skip to main content

ActiveX Stopwatch Timer Control

Here's an example of a StopWatch control implemented in Csharp and used as a control on a web client. The control needs to be register on the clients machine then added to the clients web application (in html) and then called from Javascript.

<OBJECT id="activeXTimerControl" name="activeXTimerControl" 
        classid="clsid:4F3B57CE-D9CA-41c3-ACBD-EBB2380990E7" 
        style="width: 504px; height: 182px" 
       codebase="Bin/ActiveXTimerControl.dll" class="ActiveXTimerControl.TimerControl">
       </OBJECT>

To start the Stopwatch call Start() and to end call End() then call Time() to return the result as a string or use the AddTimeToGrid to add the time to a DataGrid Control (supplied by the StopWatch control) or call AddTimeToLabel which adds the time to a textbox.
activeXTimerControl.Start();
///do someting
activeXTimerControl.Stop();
var time = activeXTimerControl.Time;
activeXTimerControl.AddTimeToGrid("my timer", time);
activeXTimerControl.AddTimeToLabel("my timer", time);

Here's the full csharp code and at the end the full html


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace ActiveXTimerControl
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[GuidAttribute("4F3B57CE-D9CA-41c3-ACBD-EBB2380990E7")]
public partial class TimerControl : UserControl, IActiveXTimerControl
{
public TimerControl()
{
InitializeComponent();
}
}
}


namespace ActiveXTimerControl
{
public interface IActiveXTimerControl
{
///
/// Add resulting timer name and time (as a string) to the GridView.
///
///
///
void AddTimeToGrid(string timerName, string time);
///
/// Add the resulting timer name and the time (as a string) to the Label.
///
///
///
void AddTimeToLabel(string timerName, string time);
///
/// Start the timer.
///
void Start();
///
/// Stop the timer, call Start() before this.
///
void Stop();
///
/// Get the Time between the Start() and Stop() in milliseconds.
///
string Time { get; }
}

partial class TimerControl : IActiveXTimerControl
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;

///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Component Designer generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this._timerDataGridView = new System.Windows.Forms.DataGridView();
this._timerNameColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this._timerTimeColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this._timerLabel = new System.Windows.Forms.Label();
this._timerTextBox = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this._timerDataGridView)).BeginInit();
this.SuspendLayout();
//
// _timerDataGridView
//
this._timerDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this._timerDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this._timerNameColumn,
this._timerTimeColumn});
this._timerDataGridView.Location = new System.Drawing.Point(6, 108);
this._timerDataGridView.Name = "_timerDataGridView";
this._timerDataGridView.Size = new System.Drawing.Size(261, 106);
this._timerDataGridView.TabIndex = 0;
//
// _timerNameColumn
//
this._timerNameColumn.HeaderText = "Timer Name";
this._timerNameColumn.Name = "_timerNameColumn";
//
// _timerTimeColumn
//
this._timerTimeColumn.HeaderText = "Time (msec)";
this._timerTimeColumn.Name = "_timerTimeColumn";
//
// _timerLabel
//
this._timerLabel.AutoSize = true;
this._timerLabel.Location = new System.Drawing.Point(223, 78);
this._timerLabel.Name = "_timerLabel";
this._timerLabel.Size = new System.Drawing.Size(129, 13);
this._timerLabel.TabIndex = 1;
this._timerLabel.Text = "Timer Name | Time (msec)";
//
// _timerTextBox
//
this._timerTextBox.Location = new System.Drawing.Point(6, 20);
this._timerTextBox.Multiline = true;
this._timerTextBox.Name = "_timerTextBox";
this._timerTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this._timerTextBox.Size = new System.Drawing.Size(261, 55);
this._timerTextBox.TabIndex = 2;
this._timerTextBox.Text = "Timer Name | Time (msec)";
//
// TimerControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this._timerTextBox);
this.Controls.Add(this._timerLabel);
this.Controls.Add(this._timerDataGridView);
this.Name = "TimerControl";
this.Size = new System.Drawing.Size(383, 313);
((System.ComponentModel.ISupportInitialize)(this._timerDataGridView)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}



#endregion



public void Start()
{
if (_stopWatch == null)
_stopWatch = new System.Diagnostics.Stopwatch();
_stopWatch.Start();
}

public void Stop()
{
_stopWatch.Stop();
}
public string Time
{
get
{
return string.Format("{0}", _stopWatch.ElapsedMilliseconds);
}
}

private System.Diagnostics.Stopwatch _stopWatch;

private System.Windows.Forms.DataGridView _timerDataGridView;
public void AddTimeToGrid(string timerName, string time)
{
if (_timerDataGridView == null)
_timerDataGridView = new System.Windows.Forms.DataGridView();
_timerDataGridView.Rows.Add(timerName, time);
this.Refresh();

}

private System.Windows.Forms.DataGridViewTextBoxColumn _timerNameColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn _timerTimeColumn;

private System.Windows.Forms.Label _timerLabel;
public void AddTimeToLabel(string timerName, string time)
{
//if (_timerLabel == null)
// _timerLabel = new System.Windows.Forms.Label();
//_timerLabel.Text += ( "\n" + timerName + " " + time);
//this.Refresh();
if (_timerTextBox == null)
{
_timerTextBox = new System.Windows.Forms.TextBox();
_timerTextBox.Text = "Timer Name | Time (msec)";
}
_timerTextBox.Text += ("\r\n" + timerName + " " + time);
this.Refresh();
}

private System.Windows.Forms.TextBox _timerTextBox;

}

}




The aspx:



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>

<OBJECT id="activeXTimerControl" name="activeXTimerControl" classid="clsid:4F3B57CE-D9CA-41c3-ACBD-EBB2380990E7" style="width: 504px; height: 182px" codebase="Bin/ActiveXTimerControl.dll" class="ActiveXTimerControl.TimerControl">
</OBJECT>


<form name="frm" id="frm">
<input type=button value="Click me" onClick="doScript();">
</form>


</body>

<script language="javascript">
function doScript()
{
activeXTimerControl.Start();

activeXTimerControl.Stop();
var time = activeXTimerControl.Time;
activeXTimerControl.AddTimeToGrid("my timer", time);
activeXTimerControl.AddTimeToLabel("my timer", time);




}
</script>

</html>



Comments

Popular posts from this blog

dotNET - Debugging

Debugging with .NET MSIL assemblies Visual Studio and debugging the CLR are different, I'll talk about both. MSIL Assemblies Assemblies compiled with .NET tools such as the CLR compiler are compiled into a file which contains MSIL (Microsoft Intermediate Language). At runtime the contents of the assembly are loaded into the CLR and ran as machine code. When you compile an assembly in debug a PDB file is generated alongside the DLL or EXE you've just created. The link between these 2 files is that the PDB contains the line numbers of the methods and classes as well as the file names of the original source code that created the assembly. When you launch the debugger in Visual Studio the assembly is loaded into the Debugger (similar to the CLR) along with the PDB file. The debugger now uses your PDB file contents to match the running code found in the assembly to locations in source files (hopefully in your present project). CLR CLR Inside Out (msdn magazine) .NET Framework Tools:...

Installer CustomAction, Debugging the CustomAction, InstallState

Custom Action The Custom Action is added to the Setup Project, select the Project node and hit the Custom Action button. This allows you add an Action to a particular phase in the Installation. But first you must create the Custom Action. To Add a Custom Action you must first have a Custom Action created, this is usually in the form of a Installer Class, this should be created in a seperate project, the Installer Class is actually one of the File Templates in the C# Projects. So it's File->New Project and select Visual C# Projects. Then add a Class Library, this will prompt you for the Class Library Types , select "Installer Class". Walkthrough - Creating Custom Action (msdn). Also here's a more comprehensive document on Setup/Installer implementations, it delves into the Registry etc Getting Started with Setup Projects (SimpleTalk). Visual Studio Setup Projects and Custom Actions (Simple Talk). Create your Installer Class and then add it as a Custom Action to the ...

dotNET - Use app.config ApplicationSettings and UserSettings

When using Settings in an Assembly or .exe you can use the Settings Designer to generate a config file using Settings. The Settings Designer provides a wrapper class which allows you to provide defaults and access the config data using Properties. But what if you're not working inside that Assembly or .exe? this presents a problem. If your loading the Assembly externally and want to access that Assembly's .config file you'll probably wish to use something in the System.Configuration namespace... unfortunately it's not of much use if you've created the .config file from the Settings Designer in Visual Studio!! This is because the Designer creates Sections and ApplicationSettings and UserSettings, the System.Configuration namespace does not provide a method to access these (it has a method to access AppSettings which are a different thing. Below I've written a workaround which locates the app.config and accesses the ApplicationSettings and UserSettings using XML i...