<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Gurock Software Support Forum - StopWatch function]]></title>
		<link>http://forum.gurock.com/topic/295/stopwatch-function/</link>
		<description><![CDATA[The most recent posts in StopWatch function.]]></description>
		<lastBuildDate>Mon, 19 Oct 2009 15:04:06 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: StopWatch function]]></title>
			<link>http://forum.gurock.com/post/900/#p900</link>
			<description><![CDATA[<p>Here&#039;s the promised example:</p><div class="quotebox"><blockquote><p>class CustomSession : Session<br />{<br />&nbsp; &nbsp; private IDictionary&lt;string, Stopwatch&gt; m_Timers;<br />&nbsp; &nbsp; private object m_TimersLock;</p><p>&nbsp; &nbsp; public CustomSession(SmartInspect parent, string name): base(parent, name)<br />&nbsp; &nbsp; { <br />&nbsp; &nbsp; &nbsp; &nbsp; m_TimersLock = new object();<br />&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; public void StartTimer(string name)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; lock (m_TimersLock)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (m_Timers == null)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_Timers = new Dictionary&lt;string, Stopwatch&gt;();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Stopwatch stopwatch = Stopwatch.StartNew();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_Timers[name] = stopwatch;<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; LogMessage(&quot;Timer started: {0}&quot;, name);<br />&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; private bool TryGetTimer(string name, out long elapsed)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; elapsed = 0;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; if (m_Timers == null)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; Stopwatch stopwatch;<br />&nbsp; &nbsp; &nbsp; &nbsp; if (!m_Timers.TryGetValue(name, out stopwatch))<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; elapsed = stopwatch.ElapsedMilliseconds;<br />&nbsp; &nbsp; &nbsp; &nbsp; return true;<br />&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; public void LogTimer(string name, string title)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; long elapsed;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; lock (m_TimersLock)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!TryGetTimer(name, out elapsed))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; LogMessage(&quot;Timer {0}: {1} ({2})&quot;, name, title, elapsed);<br />&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; public void StopTimer(string name)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; long elapsed;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; lock (m_TimersLock)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!TryGetTimer(name, out elapsed))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_Timers[name] = null;<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; LogMessage(&quot;Timer stopped: {0} ({1})&quot;, name, elapsed);<br />&nbsp; &nbsp; }<br />}</p><p>class Program<br />{<br />&nbsp; &nbsp; static void Main()<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; SiAuto.Si.Enabled = true;<br />&nbsp; &nbsp; &nbsp; &nbsp; CustomSession session = new CustomSession(SiAuto.Si, &quot;Timer&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; 100; i++)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.StartTimer(&quot;Test&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(100);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.LogTimer(&quot;Test&quot;, &quot;First message&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(100);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.LogTimer(&quot;Test&quot;, &quot;Second message&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(100);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.StopTimer(&quot;Test&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; }<br />}</p></blockquote></div><p>It basically works as you suggested. There are methods for starting/stopping a named timer and for sending log messages including the elapsed time since start. Feel free to use and modify it as needed. Let me know if you have any questions.</p>]]></description>
			<author><![CDATA[null@example.com (tgurock)]]></author>
			<pubDate>Mon, 19 Oct 2009 15:04:06 +0000</pubDate>
			<guid>http://forum.gurock.com/post/900/#p900</guid>
		</item>
		<item>
			<title><![CDATA[Re: StopWatch function]]></title>
			<link>http://forum.gurock.com/post/899/#p899</link>
			<description><![CDATA[<p>Hello Jim,</p><p>this feature is currently not included in the libraries. However, there is actually a feature in the Console which comes close to what you are asking for. There&#039;s the Details toolbox (shown by default on the left of the Console) which can display the time span between two selected log entries (see the Time Span row).</p><p>If you want to include this information directly in the log message via the library, you would need to extend the Session class with the required methods. I usually suggest using class helpers/extension methods for adding new methods. But since you need to store the timer information somewhere, it&#039;s probably a better idea to add a derived Session class (with a field to manage the timer information). I can prepare a small example on how to do this on Monday.</p><p>By the way, as an unrelated side note, there&#039;s the high-resolution time stamp feature in the libraries which provide a much higher resolution (micro-seconds) which might come in handy in your situation. High-res time stamps can be enabled with:</p><div class="codebox"><pre><code>SiAuto.Si.Resolution = ClockResolution.High;</code></pre></div><p>Hope that helps!</p>]]></description>
			<author><![CDATA[null@example.com (tgurock)]]></author>
			<pubDate>Sat, 17 Oct 2009 17:20:17 +0000</pubDate>
			<guid>http://forum.gurock.com/post/899/#p899</guid>
		</item>
		<item>
			<title><![CDATA[StopWatch function]]></title>
			<link>http://forum.gurock.com/post/897/#p897</link>
			<description><![CDATA[<p>I want to be able to start a named &quot;StopWatch&quot; and subsequently log splits to the named stopwatch. </p><br /><p>Pseudo-Code:</p><p>SiAuto.Main.CreateStopWatch( &quot;MyStopWatch&quot; );</p><p>SiAuto.Main.LogStopWatchSplit( &quot;MyStopWatch&quot;, &quot;At the begining of X&quot; );</p><p>SiAuto.Main.LogStopWatchSplit( &quot;MyStopWatch&quot;, &quot;At the ending of X&quot; );</p><p>SiAuto.Main.LogStopWatchSplit( &quot;MyStopWatch&quot;, &quot;At the begining of Y&quot; );</p><p>SiAuto.Main.LogStopWatchSplit( &quot;MyStopWatch&quot;, &quot;At the ending of Y&quot; );</p><p>SiAuto.Main.EndStopWatch( &quot;MyStopWatch&quot;);</p><p>each split logs the name, the message and the duration in millisecs since the stopwatch started. Ideally it could log the duration since the last split as well.</p><p>Create and End would log the absolute time and the duration.</p><br /><br /><p>How can I accomplish something like this with the existing API.</p><p>Thanks so much<br />Jim</p>]]></description>
			<author><![CDATA[null@example.com (Jim)]]></author>
			<pubDate>Sat, 17 Oct 2009 05:34:29 +0000</pubDate>
			<guid>http://forum.gurock.com/post/897/#p897</guid>
		</item>
	</channel>
</rss>

