Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Operators.Stopwatch/Stopwatch.cs @ 4075

Last change on this file since 4075 was 445, checked in by gkronber, 16 years ago

initial import of HeuristicLab.Operators.Stopwatch plugin (ticket #231)

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using System.Diagnostics;
28using HeuristicLab.Data;
29
30namespace HeuristicLab.Operators.Stopwatch {
31  public class Stopwatch : ItemBase {
32    private System.Diagnostics.Stopwatch stopwatch;
33    private long elapsedTicks;
34    private bool running;
35
36    public Stopwatch() {
37      stopwatch = new System.Diagnostics.Stopwatch();
38      elapsedTicks = 0;
39      running = false;
40    }
41
42    public void Start() {
43      stopwatch.Start();
44      running = true;
45    }
46
47    public void Stop() {
48      stopwatch.Stop();
49      elapsedTicks = stopwatch.ElapsedTicks;
50      stopwatch.Reset();
51      running = false;
52    }
53
54    public long ElapsedTicks {
55      get {
56        return elapsedTicks + stopwatch.ElapsedTicks;
57      }
58    }
59
60    public TimeSpan Elapsed {
61      get {
62        return TimeSpan.FromTicks(elapsedTicks).Add(stopwatch.Elapsed);
63      }
64    }
65
66    public override object Clone(IDictionary<Guid, object> clonedObjects) {
67      Stopwatch clone = (Stopwatch)base.Clone(clonedObjects);
68      if(running) clone.Start();
69      clone.elapsedTicks = elapsedTicks;
70      return clone;
71    }
72
73    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
74      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
75      XmlAttribute runningAttr = document.CreateAttribute("Running");
76      runningAttr.Value = running.ToString();
77      node.Attributes.Append(runningAttr);
78      XmlAttribute elapsedTicksAttr = document.CreateAttribute("ElapsedTicks");
79      elapsedTicksAttr.Value = elapsedTicks.ToString();
80      node.Attributes.Append(elapsedTicksAttr);
81      return node;
82    }
83    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
84      elapsedTicks = long.Parse(node.Attributes["ElapsedTicks"].Value);
85      running = bool.Parse(node.Attributes["Running"].Value);
86      if(running) stopwatch.Start();
87      base.Populate(node, restoredObjects);
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.