How to construct a Timer object in a class library

M

muzilli

Howdy all,

I would like to know how can I insert a Timer object in my class
library?
This timer object will start and stop in a determinated part or event
of my program.

I know how to do this in Delphi or using a RAD tool and insert the
Timer object in a form, but how to do in C# by hand and in a class
library (without form), I don't know.

If it is possible for you, can you show me an example?

Regards,

Marcelo Muzilli
 
G

Guest

Hey there Marcelo,

To create a timer object in your class you will need to add a reference to
System.Windows.Forms in the project and then create a new timer:

System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

hope that helps!
 
M

muzilli

Hi Justin,

will this work for .NET compact framework. This class will run for a
handheld.

Thanks,

MM
 
G

Guest

Indeed, timer is supported by the compact framework so you shouldn't have a
problem with this.
 
M

muzilli

Howdy all,

I found the solution and I will post here for future search in this
topic. I created a MyTimer class outside of my main class. Inside my
main class I created a new instance of the MyTimer class and work with
the methods Start and Stop inside my main class.

The MyTimer code is here:

/*************************************/
using System;
using System.Threading;

namespace DexCommunicator
{
/// <summary>
/// Summary description for StopWatch.
/// </summary>

public class MyTimer
{

private DateTime startTime;
private DateTime stopTime;
private bool running = false;


public void Start()
{
this.startTime = DateTime.Now;
this.running = true;
}


public void Stop()
{
this.stopTime = DateTime.Now;
this.running = false;
}


//elaspsed time in milliseconds
public double GetElapsedTime()
{
TimeSpan interval;
if (running)
{
interval = DateTime.Now - startTime;
}
else
{
interval = stopTime - startTime;
}
return interval.TotalMilliseconds;
}


//elaspsed time in seconds
public double GetElapsedTimeSecs()
{
TimeSpan interval;
if (running)
{
interval = DateTime.Now - startTime;
}
else
{
interval = stopTime - startTime;
}
return interval.TotalSeconds;
}
}
}
/*************************************/


Thanks,

Marcleo Muzilli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top