[445] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Xml;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using System.Diagnostics;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Operators.Stopwatch {
|
---|
| 31 | public class Stopwatch : ItemBase {
|
---|
| 32 | private System.Diagnostics.Stopwatch stopwatch;
|
---|
[693] | 33 | private TimeSpan elapsedTime;
|
---|
[445] | 34 | private bool running;
|
---|
| 35 |
|
---|
| 36 | public Stopwatch() {
|
---|
| 37 | stopwatch = new System.Diagnostics.Stopwatch();
|
---|
[693] | 38 | elapsedTime = new TimeSpan(0L);
|
---|
[445] | 39 | running = false;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public void Start() {
|
---|
| 43 | stopwatch.Start();
|
---|
| 44 | running = true;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public void Stop() {
|
---|
| 48 | stopwatch.Stop();
|
---|
[693] | 49 | elapsedTime += stopwatch.Elapsed;
|
---|
[445] | 50 | stopwatch.Reset();
|
---|
| 51 | running = false;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public TimeSpan Elapsed {
|
---|
| 55 | get {
|
---|
[693] | 56 | return elapsedTime + stopwatch.Elapsed;
|
---|
[445] | 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 61 | Stopwatch clone = (Stopwatch)base.Clone(clonedObjects);
|
---|
| 62 | if(running) clone.Start();
|
---|
[693] | 63 | clone.elapsedTime = elapsedTime;
|
---|
[445] | 64 | return clone;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
|
---|
| 68 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 69 | XmlAttribute runningAttr = document.CreateAttribute("Running");
|
---|
| 70 | runningAttr.Value = running.ToString();
|
---|
| 71 | node.Attributes.Append(runningAttr);
|
---|
| 72 | XmlAttribute elapsedTicksAttr = document.CreateAttribute("ElapsedTicks");
|
---|
[693] | 73 | elapsedTicksAttr.Value = elapsedTime.Ticks.ToString();
|
---|
[445] | 74 | node.Attributes.Append(elapsedTicksAttr);
|
---|
| 75 | return node;
|
---|
| 76 | }
|
---|
| 77 | public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
|
---|
[693] | 78 | long elapsedTicks = long.Parse(node.Attributes["ElapsedTicks"].Value);
|
---|
| 79 | elapsedTime = TimeSpan.FromTicks(elapsedTicks);
|
---|
[445] | 80 | running = bool.Parse(node.Attributes["Running"].Value);
|
---|
| 81 | if(running) stopwatch.Start();
|
---|
| 82 | base.Populate(node, restoredObjects);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|