Friday, October 16, 2009

WinForms/WPF Timers

I find myself using a lot of timers when doing GUI programming. Usually these timers are used to invoke a block of code only once. Sometimes there’s a good reason to delay a call, say you show a tooltip for 1sec then hide it (though in WPF animations should be used). Usually it’s to solve a tricky problem, like you have to let the callstack unwind for some dependent state to take effect. This is usually considered bad practice and there are very few times when no other legitimate techniques can be used. However when deadlines start closing in the number of timers increase. I like to think of Timers as the duct tape of GUI programming.

Using timers, especially for one-off calls is tedious and verbose

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(10);
timer.Tick += (delegate(object s, EventArgs args)
{
    timer.Stop();
    timer = null;
    DoSomethingCool();
});
timer.Start();

SetTimeout to the rescue.

The HTML DOM has a handy method called setTimeout. It's really easy to use, just pass it a function (or string) and the timeout value. That's it. There is no such time as setTimeout in .NET. But with some Extension methods combined with Lambda expressions we can get pretty close:
new Action(() => DoSomethingCool()).SetTimeout(10);

Here's the code. It comes in 2 flavors: Windows Forms and WPF

namespace SetTimeout.Wpf
{
    using System;
    using System.Windows.Threading;
 
    public static class SettimeoutDelegateExtension
    {
        /// <summary>
        /// Inspired by HTML DOM, executes a delegate via a DispatcherTimer once.
        /// </summary>
        /// <example>new Action(() => someObject.DoSomethingCool()).SetTimeout(100);
        /// </example>
        /// <remarks>Frequently things need to be executed in a timeout, but constructing a Timer is a pain especially for
        /// one-off calls. Combined with Lambda expressions this makes the whole process relativey painless.
        /// </remarks>
        /// <param name="action">Any delegate to execute</param>
        /// <param name="timeout">How long to wait to execute</param>
        /// <param name="args">Any arguments to pass to the delegate</param>
        public static void SetTimeout(this Delegate action, TimeSpan timeout, params object[] args)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = timeout;
            timer.Tick += new EventHandler(delegate(object sender, EventArgs e)
            {
                timer.Stop();
                timer = null;
                action.DynamicInvoke(args);
            });
            timer.Start();
        }
 
        /// <summary>
        /// Inspired by HTML DOM, executes a delegate via a DispatcherTimer once.
        /// </summary>
        /// <example>new Action(() => someObject.DoSomethingCool()).SetTimeout(100);
        /// </example>
        /// <remarks>Frequently things need to be executed in a timeout, but constructing a Timer is a pain especially for
        /// one-off calls. Combined with Lambda expressions this makes the whole process relativey painless.
        /// </remarks>
        /// <param name="action">Any delegate to execute</param>
        /// <param name="timeout">How long to wait to execute in milliseconds</param>
        /// <param name="args">Any arguments to pass to the delegate</param>
        public static void SetTimeout(this Delegate action, int timeout, params object[] args)
        {
            SetTimeout(action, TimeSpan.FromMilliseconds(timeout), args);
        }
    }
}

p.s. I hope you enjoy the pretty print. I spent way too much time on it

Win 64 VM on Win 32 Host

How bad-assed is this??

32-64

Friday, February 20, 2009

Chuck’s Low Flow Seinfeld Hair

Let’s take a break from the techie stuff for a sec and focus on one of my other passions:TV. Specifically “Chuck”. Even more specifically, Chuck’s hair. It’s changed a little bit since season 1 and now looks like this:

image

This hairstyle strikes me as familiar…

image

Looks like he got a low-flow shower head installed since season 1.

Thursday, August 14, 2008

Old School Wait Animation

I noticed today something a little funny about the Visual Studio 2008 SP1 setup screen:

Notice the little black line at the below the 2nd progress bar at the bottom right? It's actually an old school ASCII wait animation that consists of displaying /-\|.

I'm not sure why they felt a need to add it under a progress bar, the fact that there is a progress bar is kind of a tip off that I need to wait. But like I said, it's funny and may have just been some humor on a programmer's part.

Monday, August 11, 2008

Do I have enough crap attached to my notebook?


Believe it or not I actually use all these items.
On the left side going top to bottom:
  1. Notebook Power Supply
  2. HDMI cable going to my external monitor
  3. Two USB cables going to a Maxtor External Hard Drive
  4. ESATA cable attached to another hard drive. The ESATA card is plugged in to the PCMCIA slot
On the Right
  1. Ethernet cable for network
  2. iPhone USB cable
  3. USB cable going to the external monitor's USB hub for keyboard and mouse
  4. Headphones

Monday, February 18, 2008

RainbowText 2.0

This is the upgrade 8 years in the making!

I was going over some of my old projects and I realized the old version of RainbowText was in sore need of an upgrade.

Well a few hours later version 2.0 was ready to go. The biggest difference is this one supports all the latest major browsers. In order to do this I had to ditch the IE Behavior. However the new version is just as easy to use, and it is a bit clearer, in my opinion.

RainbowText is one of those scripts that annoy the hell out of most people, and a handful of people absolutely love it. Which are you?

View a demo and download the code here.

Wednesday, February 13, 2008

VS2008 Web Developer Hotfix

http://weblogs.asp.net/scottgu/archive/2008/02/08/vs-2008-web-development-hot-fix-roll-up-available.aspx
I would seriously recommend this to anyone who does web development with VS2008. Most of these are performance fixes, almost all of these I've personally experienced:

  • Source editor freezes for a few seconds when typing in a page with a custom control that has more than two levels of sub-properties

    Yep. Got this when working with Infragistics controls. Even hitting the down arrow to scroll through the intellisense list of attributes caused this delay. Very annoying.

  • “View Code” right-click context menu command takes a long time to appear with web application projects

    Got this too, though I didn't notice too much of a difference after the hotfix. I guess my PC's just slow on this one.

  • Visual Studio has very slow behavior when opening large HTML documents.
  • Visual Studio has responsiveness issues when working with big HTML files with certain markup
    Check and check. I still get unresponsiveness issues, but mostly now just when a document loads, whereas before it would happen constantly. I noticed if there was a lot of invalid HTML this would cause problems, also when using custom namespaced elements (running on the client, not ASP.NET controls) the editor seemed to have problems with these kinds of documents too.

  • The Tab/Shift-Tab (Indent/Un-indent) operation is slow with large HTML selections
    That's always been the case, and not just in HTML but in any code section that has intellisense parsing.

  • Slow typing in design view with certain page markup configurations
    Got this one too, again when working with Infragistics Controls. Typing in labels directly in TDs in design view was REAALLLY slow, and it didn't seem to buffer properly so you couldn't just type the whole thing then wait.

  • When opening a JavaScript file, colorization of the client script is sometimes delayed several seconds.
    Sometimes? Anything more than a trivial document would exhibit this behavior.