<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>jaredpar's WebLog : WinForms</title><link>http://blogs.msdn.com/jaredpar/archive/tags/WinForms/default.aspx</link><description>Tags: WinForms</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Properly handling a WinForms Timer event</title><link>http://blogs.msdn.com/jaredpar/archive/2008/10/27/properly-handling-a-winforms-timer-event.aspx</link><pubDate>Mon, 27 Oct 2008 15:00:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9017488</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/9017488.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=9017488</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=9017488</wfw:comment><description>&lt;p&gt;The WinForms &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx"&gt;Timer&lt;/a&gt; class allows the user to perform a particular action at a set interval.&amp;#160; Timer objects fire a &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.tick.aspx"&gt;Tick&lt;/a&gt; event at the set time which users can easily respond to.&amp;#160; This is very useful if a developer wants to check for a particular condition say every 2 seconds ( for the remainder of this article I'm going to use 2 seconds as a practical example even though it's really any arbitrary time period).&lt;/p&gt;  &lt;p&gt;Occasionally users are surprised to find that the Tick event will fire much faster than they are expecting.&amp;#160; Instead of waiting for 2 seconds between calls, they event will fire almost immediately after one is finished processing. &lt;/p&gt;  &lt;p&gt;What's going on here is a side effect of how this event works under the hood.&amp;#160; The interval for the timer event is calculated in real world time.&amp;#160; So quite literally every 2 seconds Windows will consider the internal reached and will issue a new tick message.&amp;#160; The next time a WinForms event is not executing developer code a tick event is raised [1].&amp;#160; &lt;/p&gt;  &lt;p&gt;So imagine we had the following code.&amp;#160; &lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Private Sub &lt;/span&gt;OnTimerTick() &lt;span style="color: blue"&gt;Handles &lt;/span&gt;m_timer.Tick
    RunSomeOperation()
&lt;span style="color: blue"&gt;End Sub&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Consider what happens if RunSomeOperation takes longer than 2 seconds.&amp;#160; The Tick event is fired in real time so while we're in the middle of RunSomeOperation, another Tick event is being queued up for processing.&amp;#160; As soon as we leave OnTimerTick we're back in WinForms code which sees a Tick event and promptly raises it which puts us right back in OnTimerTick.&lt;/p&gt;

&lt;p&gt;This is contrary to what most people expect.&amp;#160; Most people expect the Tick event to fire 2 seconds after their code is finished executing.&amp;#160; &lt;/p&gt;

&lt;p&gt;To work around this developers should stop the timer when processing a timer event.&amp;#160; Just before exiting the event handler, re-enable the timer.&amp;#160; This will cause Windows to start calculating the interval from the start.&amp;#160; This has the effect of making the timer event fire 2 seconds after developer code stops executing.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Private Sub &lt;/span&gt;OnTimerTick() &lt;span style="color: blue"&gt;Handles &lt;/span&gt;m_timer.Tick
    m_timer.Stop()
    &lt;span style="color: blue"&gt;Try
        &lt;/span&gt;RunSomeOperation()
    &lt;span style="color: blue"&gt;Finally
        &lt;/span&gt;m_timer.Start()
    &lt;span style="color: blue"&gt;End Try
End Sub&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;[1] This is not 100% true.&amp;#160; It's really whenever the Application begins to pump messages again.&amp;#160; Message pumping, more specifically when it does and does not occur, is too involved for this discussion.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9017488" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/WinForms/default.aspx">WinForms</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Gotcha/default.aspx">Gotcha</category></item><item><title>AutoSize and DockStyle.Fill don't mix</title><link>http://blogs.msdn.com/jaredpar/archive/2007/11/21/autosize-and-dockstyle-fill-don-t-mix.aspx</link><pubDate>Wed, 21 Nov 2007 08:29:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6450156</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/6450156.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=6450156</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=6450156</wfw:comment><description>&lt;p&gt;The title of this post essentially says it all.&amp;#160; AutoSize and DockStyle.Fill don't mix well together.&amp;#160; Both properties exist to describe the size relationship relative to the rest of the control but they do so in conflicting ways.&lt;/p&gt;  &lt;p&gt;AutoSize is a property describing the size of a control relative to it's contents.&amp;#160; Setting this to True will generally cause a control to resize itself so that it takes up only enough room to display it's contents.&amp;#160; &lt;/p&gt;  &lt;p&gt;DockStyle.Fill is a property describing the size of a control relative to the size of it's container.&amp;#160; A control will resize to fit all of the empty space in it's parent container with this property set. &lt;/p&gt;  &lt;p&gt;In effect these properties represent opposite ways of describing size.&amp;#160; Most controls will prevent you from setting conflicting settings in the designer.&amp;#160; However it's difficult to prevent you from setting conflicting settings between a container and it's contents.&lt;/p&gt;  &lt;p&gt;For instance, one combination is the following.&amp;#160; This may seem a little odd at first.&amp;#160; It commonly happens when you are trying to embed a non-autosizing control inside a Container that supports AutoSize.&amp;#160; For instance if you are trying to place a TextBox inside of a FlowLayoutPanel&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Container&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;AutoSize = true&lt;/li&gt;      &lt;li&gt;AutoSizeMode = GrowAndShrink&lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;Containee&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Dock = DockStyle.Fill&lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;If you try this code it will often cause the Container Control to shrink to nothing.&amp;#160; Why?&amp;#160; In affect both controls are asking each other how big they should be and with no one having the deciding factor they agree on ... nothing. &lt;/p&gt;  &lt;p&gt;Instead for this scenario you should leave the Containee to DockStyle.None and manually set the size.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6450156" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/WinForms/default.aspx">WinForms</category></item></channel></rss>