Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Dev Blog - Johan Danforth

I'm Johan Danforth and this is my dev blog - a mix of .NET, ASP.NET, Rest, Azure and some other random coding stuff.

  • [Team System] Migrating Code From VSS to VS2005 and Team Foundation Server

    UPDATE: I really, really recommend that you check out this GUI for VSSConverter at http://www.epocalipse.com/blog/2006/04/06/vssconverter-gui/ It was created by Eyal Post and it works like a charm! 

    We recently tried to migrate old VS2003 code from VSS to VS2005 and Team Foundation Server using the vssconverter utility that ships with Team System, and it worked pretty well... after a while.

    It's pretty straight forward if you follow the guideance on this page, http://msdn.microsoft.com/en-us/library/ms253060.aspx, but I really recommend you make sure the old code and VSS database is backed up first and that you try the migration to a test-project first.

    We set up a local VSS database on a client machine and moved (via archiving) the code to that local database first. Then we ran the analyze, test migration, tweaked the user mapping file and then finally kicked off the migration to the production TFS project. It worked without any major problems except that we had to be logged in as a user on the domain and with proper right to the team project. But it's all in the TFS documentation on MSDN... somewhere :D

    What may be somewhat itchy is the work you may get into after that, when you open up old solution and project files. It may be that VS2005 fails to load the old project files, but try to do a "get latest" and check out the .vbproj and .csproj files, then try to reload the projects. Good luck! 

    UPDATE: It looks as if the best way to open up a migrated solution is to locate the solution file in the Source Control Explorer window and double-click it. I will probably ask you to re-bind the projects, so do that and they should "connect" automatically. Make sure you have "Visual Studio Team Foundation Source Control" selected as your Source Control Plug-in in Tools->Options. It happens that the plug-in selection reverts back to VSS sometimes when the solution is opened.

  • Colibri Type Ahead

    Jan-Erik says:
    I can't imagine how I managed before I installed Colibri. It's so super easy to start programs now. Just press CTRL+SPACE and write the program name. I rarely ever goes to the Start-menu anymore... It's really super.
    I just installed it myself and it's... cute!
  • [Team System] How to Delete a TFS Team Project

    There are several ways to delete a TFS team project, but not via the Team Explorer. Steven Smith writes:

    If you need to delete a Team System Project you need to do it through a command line utility that is installed with Team Explorer.  In my case, I created a test project but wanted to also test project deletion.  There is no way to delete a project from Team System except through the command line tool, TFSDeleteProject.exe.  This utility is in the c:\program files\Microsoft Visual Studio 8\Common7\IDE\ folder by default.  To delete a project, use the following syntax:

    TFSDeleteProject /server:ServerName ProjectName

    Julien Lavigne got a utility which does the same thing via a web service.

  • [Team System] How to Delete a TFS Build Type

    Something I read on MSDN about deleting Build Types (because there's no obvious way to do it in the Team Explorer:

    To delete a build type
    1. On the View menu, click Other Windows and then click Source Control Explorer.
    2. In the Folder pane of the Source Control Explorer, locate the build type that you want to delete in the ${Team Project}\Team Build Types\{Build Type Name}\TeamBuild.proj tree. The build type is represented by the TeamBuild.proj file, but you need to delete the folder as well.
    3. With the desired build type folder selected, choose Delete from the shortcut menu. You will also need to check in the changes that were made to the directory. The build type entry is deleted and the action removes all underlying XML files.

    Good shortcut for how to manage Build Types in TFS -> Working with Build Types in Team Build 

    I also found good info about deleting build types on the Vertigo's Team System blog -> http://blogs.vertigosoftware.com/teamsystem/archive/2006/05/03/Deleting_Team_Build_Types_and_Team_Builds.aspx

     

  • [SQL] Using a Variable TOP Clause

    This is a simple one, but still worth mentioning. Say you want to select a variable number of rows in a SELECT statement and don't want to use "dynamic SQL" to do that but rather use a SQL Parameter - no problems. In SQL Server 2005 you can type a query like this:

    SELECT TOP (@number) Title,Body,Author,Created,LastModified FROM Pages ORDER BY LastModified DESC

    Note that the parenthesis around @number is important, or you'll end up getting this error:

    Incorrect syntax near '@number'.

    If you're stuck with an older version of SQL Server, there are several ways to work around that problem. You could for example set the ROWCOUNT, as ROWCOUNT supports a variable:

    SET ROWCOUNT @number; SELECT Title,Body,Author,Created,LastModified FROM Pages ORDER BY LastModified DESC; SET ROWCOUNT 0

    A good place to look for SQL syntax information is here and here.
  • [Team System] Looking at Team Foundation Server

    I've not been blogging that much lately - and for good reasons. I switched work a week ago, and many of you know how that is. Full speed ahead, lot of new info to dig into and new things to learn. It's been an inspiring week and it feels really good.

    Anyway, I've started to look more in detail at Team System and especially the Foundation Server parts. We plan to introduce it at work and really start using it to it's full extent with custom project templates and work items. I've been looking at most of the videos at TeamStudioRocks, and even though they are from the beta versions, that site is worth a visit or two. Other good websites for Team System and TFS related info is the Rob Caron blog and of course the Visual Studio Team System User Education (phew) blog.

  • My Cat Sponsored by Google?

    Amazing... I was installing the trial version of Winzip 10 to zip up a bunch of files for a big backup and I was just about to uncheck the "install Google..." options because I already got the Windows Live stuff installed and it works pretty well. As I was reaching for that checkbox my cat jumps up on the table and hits Enter and starts to install everything. Now I got search deskbars and toolbars like everywhere... how useful... not.

    After I've uninstalled certain components I'm gonna have a talk with that cat.

  • Kid's Programming Language - KPL

    If you like coding and simple games and perhaps want to teach your kids (or yourself for that matter) how to code games - check out Kid's Programming Language. It's free, it's based on .NET 1.1, it comes with a decent color-coded editor and a bunch of sample game code, sounds and sprites. Including a fully functional Missile Command game in around 1500 lines of code. An Asteroids looking game can be downloaded.

    As you understand, the main focus of KPL seems to be to learn how to code by maknig games. Not a bad idea. I spent a few hours playing around with it and it's seriously fun. Anyone who knows just a little coding can make their own little game and version 2 of KLP will have full support for 3D.

  • Ultimate List of Free Windows Software from Microsoft

    Found this blog post on "The Road to Know Where" - a huge list of free and sometimes very useful tools and apps from Microsoft:

    Ultimate List of Free Windows Software from Microsoft
    Microsoft has over 150 FREE Windows Programs available for download -- but finding them all is extremely difficult. Until now, thanks to the Road to Know Where!

    A few favorites on that list are Open Command Window Here and Alt-Tab Replacement.

  • [.NET 2.0] Detecting Idle Time with Mouse and Keyboard Hooks

    I thought I could share some piece of code I wrote a couple of years ago to detect if a user is idle or not. I used it on a chat client I developed because we were not allowed to use Messenger at the site. So the way to do it is to set up a coupld of low-level Windows hooks to detect mouse and keyboard activity. Obviously this involves a couple of DLL Import statements and you have to be careful to UnHook everything when the application exits as we're dealing with unmanaged code here. That's why I implemented the IDispose pattern for it.

    This code is actually a piece of cake to implement if you use the ClientIdleHandler class (code below). First a look at the sample Windows Form which is made up of a lable indicating if the user is idle or active, and a timer set to 1 second. Each time the timer fires, it checks the bActive value in the ClientIdleHandler to see if the user has been active since the last check. I also added a progress bar on it (0 to 10) to give a visual indication on the idle seconds count.

    Note: This code has not been tested in production systems and if you decide to use it, do that at your own risk. This code has been running pretty fine for the last 2 years, but on .NET 1.1. I don't know how well it behaves on .NET 2.0 even though it seems to work well. Also note that this code is not optimized in anyway and you may want to implement the static bActive variable different, perhaps raise events from the ClientIdleHandler class instead of using a timer like I do. Anyway, you have been warned :)

    Form1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace MouseAndKbdHookSample
    {
        
    public partial class Form1 : Form
        {
            
    private ClientIdleHandler clientIdleHandler = null;
            
    private int iIdleCount = 0; //keeps track of how many seconds the user has been idle
            private const int IDLE_THRESHOLD = 10; //how many seconds should pass before the app goes into idle mode

            public Form1()
            {
                InitializeComponent();

                
    //start client idle hook
                clientIdleHandler = new ClientIdleHandler();
                clientIdleHandler.Start();
            }

            
    private void timer1_Tick(object sender, EventArgs e)
            {
                
    if (ClientIdleHandler.bActive)    //indicates user is active
                {
                    
    //zero the idle counters
                    ClientIdleHandler.bActive = false;
                    iIdleCount = 0;

                    
    //change user status
                    if (IdleLabel.Text == "IDLE")
                        IdleLabel.Text =
    "ACTIVE";

                }
                
    else    //user was idle the last second
                {
                    
    //check if threshold was reached
                    if (iIdleCount >= IDLE_THRESHOLD)
                    {
                        
    //change to IDLE and stop counting seconds
                        if (IdleLabel.Text == "ACTIVE")
                            IdleLabel.Text =
    "IDLE";
                    }
                    
    else //increase secs counter
                        iIdleCount++;
                }

                
    //some visual goo added
                progressBar1.Value = iIdleCount;
            }
        }
    }

    I added some code into the Dispose() method of the form, to help close the hooks, like this:

              //Dispose of our client idle handler
              if (clientIdleHandler != null)
                  clientIdleHandler.Close();

    Then there is this ClientIdleHandler class which does it all. I added enough comments in there I hope to explain what it does. If you want to read more on these hooks and how to use them from .NET there is always Google out there and a few KB articles on MSDN which helped me out. Like this one for example. Some people may recognize parts of the code in this class from a bunch of code sample that can be found on the Internet, still this kind of functionality is still asked for by people in forums.

    ClientIdleHandler.cs

    using System;
    using System.Runtime.InteropServices;

    namespace MouseAndKbdHookSample
    {
        
    /// <summary>
        /// Write something here sometime...
        /// </summary>
        public class ClientIdleHandler : IDisposable
        {
            
    //idle counter
            public static bool bActive = false;
            
    // hook active or not
            static int hHookKbd = 0;
            
    static int hHookMouse = 0;

            
    // the Hook delegate
            public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
            
    //Declare MouseHookProcedure as HookProc type.
            public event HookProc MouseHookProcedure;
            
    //Declare KbdHookProcedure as HookProc type.
            public event HookProc KbdHookProcedure;

            
    //Import for SetWindowsHookEx function.
            //Use this function to install thread-specific hook.
            [DllImport("user32.dll", CharSet = CharSet.Auto,
                 CallingConvention =
    CallingConvention.StdCall)]
            
    public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
                
    IntPtr hInstance, int threadId);

            
    //Import for UnhookWindowsHookEx.
            //Call this function to uninstall the hook.
            [DllImport("user32.dll", CharSet = CharSet.Auto,
                 CallingConvention =
    CallingConvention.StdCall)]
            
    public static extern bool UnhookWindowsHookEx(int idHook);

            
    //Import for CallNextHookEx
            //Use this function to pass the hook information to next hook procedure in chain.
            [DllImport("user32.dll", CharSet = CharSet.Auto,
                 CallingConvention =
    CallingConvention.StdCall)]
            
    public static extern int CallNextHookEx(int idHook, int nCode,
                
    IntPtr wParam, IntPtr lParam);

            //Added all the hook types here just for reference
            
    public enum HookType : int
            {
                WH_JOURNALRECORD = 0,
                WH_JOURNALPLAYBACK = 1,
                WH_KEYBOARD = 2,
                WH_GETMESSAGE = 3,
                WH_CALLWNDPROC = 4,
                WH_CBT = 5,
                WH_SYSMSGFILTER = 6,
                WH_MOUSE = 7,
                WH_HARDWARE = 8,
                WH_DEBUG = 9,
                WH_SHELL = 10,
                WH_FOREGROUNDIDLE = 11,
                WH_CALLWNDPROCRET = 12,
                WH_KEYBOARD_LL = 13,
                WH_MOUSE_LL = 14
            }

            
    static public int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
            {
                
    //user is active, at least with the mouse
                bActive = true;
                
                
    //just return the next hook
                return CallNextHookEx(hHookMouse, nCode, wParam, lParam);
            }


            
    static public int KbdHookProc(int nCode, IntPtr wParam, IntPtr lParam)
            {
                
    //user is active, at least with the mouse
                bActive = true;

                
    //just return the next hook
                return CallNextHookEx(hHookKbd, nCode, wParam, lParam);
            }

            
    public void Start()
            {
                
    if (hHookMouse == 0)
                {
                    
    // Create an instance of HookProc.
                    MouseHookProcedure = new HookProc(MouseHookProc);
                    
    // Create an instance of HookProc.
                    KbdHookProcedure = new HookProc(KbdHookProc);

                    
    //register a global hook
                    hHookMouse = SetWindowsHookEx((int)HookType.WH_MOUSE_LL,
                        MouseHookProcedure,
                        (System.
    IntPtr)Marshal.GetHINSTANCE(
                        System.Reflection.
    Assembly.GetExecutingAssembly().GetModules()[0]),
                        0);
                    
    //If SetWindowsHookEx fails.
                    if (hHookMouse == 0)
                    {
                        Close();
                        
    throw new ApplicationException("SetWindowsHookEx() failed");
                    }
                }

                
    if (hHookKbd == 0)
                {
                    
    //register a global hook
                    hHookKbd = SetWindowsHookEx((int)HookType.WH_KEYBOARD_LL,
                        KbdHookProcedure,
                        (System.
    IntPtr)Marshal.GetHINSTANCE(
                        System.Reflection.
    Assembly.GetExecutingAssembly().GetModules()[0]),
                        0);
                    
    //If SetWindowsHookEx fails.
                    if (hHookKbd == 0)
                    {
                        Close();
                        
    throw new ApplicationException("SetWindowsHookEx() failed");
                    }
                }
            }

            
    public void Close()
            {
                
    if (hHookMouse != 0)
                {
                    
    bool ret = UnhookWindowsHookEx(hHookMouse);
                    
    //If UnhookWindowsHookEx fails.
                    if (ret == false)
                    {
                        
    throw new ApplicationException("UnhookWindowsHookEx() failed");
                    }
                    hHookMouse = 0;
                }

                
    if (hHookKbd != 0)
                {
                    
    bool ret = UnhookWindowsHookEx(hHookKbd);
                    
    //If UnhookWindowsHookEx fails.
                    if (ret == false)
                    {
                        
    throw new ApplicationException("UnhookWindowsHookEx() failed");
                    }
                    hHookKbd = 0;
                }
            }

            
    public ClientIdleHandler()
            {
                
    //
                // TODO: Add constructor logic here
                //
            }
            #region IDisposable Members

            
    public void Dispose()
            {
                
    if (hHookMouse != 0 || hHookKbd != 0)
                    Close();
            }

            #endregion
        }
    }

    That's it folks! I hope it helps someone.