Hi,

here is a new task. Generally it's a task to measure the
time between two calls of it.

-sa

-- 
sa at programmers-world dot com http://www.livingit.de
Internet sites:
  http://www.not2long.net - Make long links short
  Boomarks online: http://www.mobile-bookmarks.info
using System;
using NAnt.Core;
using NAnt.Core.Util;
using NAnt.Core.Attributes;
namespace TimeTasks {
  /// <summary>Take the time</summary>
  /// <remarks>
  ///   <para>
  ///     Time is measured from first call to recurring calls.
  ///   </para>
  ///   <para>
  ///     Name attribute is used to identify which stopwatch to use.
  ///   </para>
  ///   <example>
  ///     <para>Measure time to sleep with the <see cref="Tasks.SleepTask"/> 
task</para>
  ///   </example>
  /// </remarks>
  [TaskName("stopwatch")]
  public class Stopwatch : Task {
    private const long TICKTIME = 10000000;

    private string _stopWatchName = "";

    /// <summary>the timer </summary>
    [TaskAttribute("name", Required=true)]
    [StringValidator(AllowEmpty=false)]
    public string StopWatchName {
      get { return _stopWatchName; }
      set { _stopWatchName = StringUtils.ConvertEmptyToNull(value); }
    } //  Name

    #region constructor
    public Stopwatch() {
      return;
    } // Time()
    #endregion


    /// <summary>Save time or show ticks</summary>
    protected override void ExecuteTask() {

      long actualTicks = DateTime.Now.Ticks;

      if (this.Project.Properties.Contains(_stopWatchName + "SW")) {
        long ticksRun = actualTicks - 
long.Parse(this.Project.Properties[_stopWatchName + "SW"]);

        long nanoSeconds = (ticksRun - ((ticksRun / TICKTIME) * TICKTIME)) * 100;
        long microSeconds = nanoSeconds / 1000;
        nanoSeconds -= (microSeconds * 1000);
        long miliSeconds = microSeconds / 1000;
        microSeconds -= (miliSeconds * 1000);
        ticksRun /= TICKTIME;
        long minutes = ticksRun / 60;
        long seconds = ticksRun - (minutes * 60);

        // this.Log(Level.Info, "{0}Timer run for {1}m {2}s {3}ms {4}mis {5}ns", 
this.LogPrefix, minutes, seconds, miliSeconds, microSeconds, nanoSeconds);
        this.Log(Level.Info, "{0}Timer run for {1}m {2}s {3}ms", this.LogPrefix, 
minutes, seconds, miliSeconds);
      } else {
        this.Project.Properties[_stopWatchName + "SW"] =  actualTicks.ToString();
      } // if
      return;
    } // ExecuteTask()

  } // Stopwatch
} // TimeTasks

Reply via email to