Tuesday, May 23, 2006

Debugging Windows Services in Visual Studio

Last week I started writing a Windows Service in Visual Studio for some build and test automation programs. What I have always found annoying (but understandable) about windows services is the debugging. Because your code is set up to run as a service when you select the Windows Service template in VS that means you have to install the service, start it, and then attach to the process in the debugger instead of being able to click the "start debugging" button on the debugger toolbar like I'm used to with other project types.

Previously, I followed the advice in this article by creating a setup program that easily allowed me to install and uninstall the service directly from the Solution Explorer in VS. However I still have to either call System.Diagnostics.Debugger.Break() in my code (and remember to remove it when I'm done!) or attach to the process in order to debug it.

There's an easier way. If you look in the Main entry point of the service you will see how the service is loaded. (In C# this is in the Program.cs file that's created for you and in VB.NET this code resides in the service's Designer.vb file.) You can modify this code by adding a DEBUG compiler directive to control how the process starts:
    <MTAThread()> _
    Shared Sub Main()
#If DEBUG Then
        ' Start the process as a non-service for debugging only.
        ' Stop the debugger to stop the process.

        Dim service As New MyService 
        service.Execute() 

        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)
#Else
        Dim ServicesToRun() As System.ServiceProcess.ServiceBase

        ' More than one NT Service may run within the same process. To add
        ' another service to this process, change the following line to
        ' create a second service object. For example,
        '
        '   ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
        '
        ServicesToRun = New System.ServiceProcess.ServiceBase() {New MyService}

        System.ServiceProcess.ServiceBase.Run(ServicesToRun)
#End If
    End Sub
Now when your configuration is set to debug, you can hit F5 to debug like normal. Stop the debugger to stop the process. Keep in mind that the OnStart and OnStop methods of your service will not run so you should break your functionality into a callable method on your service class (in this example, I called it "Execute()"). Have fun!

Wednesday, May 17, 2006

Programming Computers

Today I spoke to some of my mom's computer students about programming at the school where she teaches. What a change from last week where I'm teaching other developers advanced programming topics at DevTeach. Today I'm encouraging 14 year olds to get into the field and demo-ing Visual Basic 2005 Express and the starter kits. We had a blast poking around the black jack starter kit.

I started the presentation by introducing myself and telling them the kind of software products we write at GiftRAP Corporation. I told them that I worked at Microsoft a couple years ago on the Visual FoxPro team in the developer division. Then I explained what a Microsoft MVP is and that I still get to contribute ideas to Microsoft even though I don't work there.

Since I wanted to encourage them to start programming, I went through some of my history as a kid learning to program. I showed them my first computer, the Atari 400, and my first programming language; it was 1980 and I was 8 years old. I got some bright eyes in a couple kids when I said that. I got some gasps when I told them it retailed for $595.00. I realize now that my parents really sacrificed to buy that computer for me; they didn't spend that type of money back then. Then I skipped to 1986, the year I was in 8th grade and showed them the Amiga 2000 HD. I think HD meant that you got a hard drive, something I didn't have on my Franklin "IBM PC clone". I wanted to draw all day when I was a young teen but I loved computers, so I begged my mom for an Amiga; the best in computer graphics at the time. And as a bonus, it came with an 8086 motherboard so I could run MS-BASIC. With 1MB RAM I was cooking with gas. Finally I showed them a picture of a few of the machines I have today including my 2 year old SmartPhone. I waved it in the air and told them it had 32 times more memory than my Amiga 2000. A couple kids laughed and a couple wanted me to cut to the chase -- let's program already!

I opened up VS and selected the card game starter kit template and we started tweaking it. I changed the card skins to a snoopy face and made Player 1 a picture of the president. They laughed, they stared, they asked questions. I'd say they seemed pretty darn interested (for teenagers). I pointed them to a VS Express Edition game programming site that looked pretty fun. I think I proved to them that programming is not only a scientific process but also a creative one. Mom said to save the presentation and she wants me back earlier in the year next year. I'm really glad I had the opportunity to do this with the kids today, it's almost harder to do these types of lessons than the professional sessions I do. It's cool, I think some things I was saying to them sunk in and hopefully will make a tiny impression on their future career choices. It was a very rewarding talk for me.

Here's the links I gave them. This is VB Express free edition:
http://msdn.microsoft.com/vstudio/express/vb/
These are the starter kits:
http://msdn.microsoft.com/vstudio/express/vb/starterkit/default.aspx
Here's the sweepstakes and more games:
http://www.upgradeyourgame.com/
Links to beginer videos and learning links:
http://msdn.microsoft.com/vstudio/express/vb/next/default.aspx
http://msdn.microsoft.com/vstudio/express/vb/learning/default.aspx
Here's the community forum:
http://forums.microsoft.com/msdn/showforum.aspx?forumid=24&siteid=1

Tuesday, May 16, 2006

"Best and Worst of .NET" Interviews at DevTeach

Mario Cardinal interviewed the speakers at DevTeach on what we thought were the best and worst parts of Microsoft .NET. Mario snuck up on me and put me on the spot. If he asked me again today I would probably have thought of completely different things to say. There's just so much I like ;-). Check it out here!

Saturday, May 13, 2006

Amazing Countrysides

So now that DevTeach is over, Alan and I are in beautiful Vermont staying with Julie Lerman and her husband Rich. What a great feel this countryside has, rolling green hills and birch trees everywhere. Living in the East Bay Area of California we have lots of rolling green hills but the grass is very different; much longer, and dies in the summer. We also have oak trees, not these white wood birch so it's so cool to see a different countryside. (Although all this birch is a little Blair Witch-y for me at night ;-P) It's my first time in Vermont but I'm sure I'll be back.

Wonderful Montreal in May

DevTeach is over; what a great time! Montreal was absolutely beautiful. No rain, nice and warm, in the 70s. I couldn't have asked for anything more. My sessions went very well and I got great feedback from the attendees on the conference as a whole. For session coverage check out the UT. The focus was on Data and Alan gave a great keynote on LINQ. If you haven't done it already, download the latest CTP and start playing with it now. It really will change the way you program... everything.