Long-time readers will recall that I often post here how much I love .NET. You can go back in the posting archives of this site and see how it thrills me when things that I want to do are made simpler with .NET (compared with other programming environments I am used to).
Today was not a good day in that regard.
Where do I begin? Well, first of all, I was writing a simple Windows Forms program to call a stored procedure and write the dataset returned out to a text file. Should be simple to do.
Well, I tried to be a bit fancy right off the bat. I don't know why I did it, but I lost a couple of hours of productivity and I still don't understand what the big deal is.
Here's what I tried to do: I wanted execution time to be displayed on screen as the program ran. You know, every second the text will update to say “1 second”, “2 seconds”, “3 seconds”, etc. Easy right? I just add a timer component to the form and have the Tick event update the text in a label.
Well, I tried to run the program in debug mode, by stepping through it line by line. When I happened upon my SqlConnection.Open() statement, the debugger froze. “There is no source code for the current location.” Hmm. I was using Step Over, not Step Into, so it should not be trying to go into the SqlConnection object code. Is my SQL connection setup OK? Check my connection string. Database is running. Add a generic exception handler to the catch statement. Tried commenting a bunch of stuff out. Moved things around. Nothing worked. The program always froze on the SqlConnection.Open() statement.
I called a friend over, and showed the problem to him. We both spent 20 minutes trying different ways of doing the same thing, to no avail.
Finally, I checked Google. It led me right away to this MSDN Knowledge base article. Ah, you cannot access the SqlConnection from inside a timer event in Debug mode. But the article suggested a work around. Instead of using a Timer component, I could use the System.Timers.Timer class.
So I reworked my code to use that class instead. The method and property names are slightly different, so it wasn't just one-line change. Ran it again.
This time it worked. But... the program continued to act strangely in Debug mode. When I tried to read the dataset using a SystemDataReader.Read() call, it froze again.
So finally, I pulled out all the timer events. Not the best solution, but it worked fine after that.
Anyways, .NET's flakiness with Timer events cost me a couple of hours today. While most of the time it is fun to be a programmer in this tool, today it was not fun.
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.