Category Archives: Technical

These posts are technical in nature. The casual reader may not care about these posts. These are for my fellow geeks.

Online Training: Beginners Guide to VB.NET

Microsoft Learning has put together a great series of training courses which are available now at a substantial discount.  Take advantage of the deal while it lasts (sale ends June 30th)!

https://www.microsoftelearning.com/visualstudio2005/#upgradingfromVB60

Also, don’t forget to check out the free training videos for VB.NET Express Edition. They provide a great starting point for anyone looking to break into the Visual Basic .NET market.

http://msdn.microsoft.com/vstudio/express/vb/learning/

Snagged from Sahil: Bad programmer diseases

Sahil has once again shown true insight and provided a list of common diseases found in our industry. Do you have any of these symptoms?


http://codebetter.com/blogs/sahil.malik/archive/2006/05/03/64255.aspx

Mort, Elvis and Einstein don’t exist

Much has been said about the Mort, Elvis and Einstein controversy over the past few years (yes, years).  The past few days have been no exception.  One of our MVPs is apparently upset about them, and apparently some employees are not too happy about it either.  I’ve waited several days to comment until the screaming stopped.  Now that it has, it’s my turn to weigh in.  Most of this content about this topic is completely off-base and unfounded.  “Why?”, you might ask.  That’s because Mort, Elvis, and Einstein don’t exist.  That’s right, there is no one person on this planet that is meant to be exhibited by these personas.  The names depicted here are meant to represent behaviors — not people.  Trying to pigeon-hole people into one of these areas is just a misrepresentation of what the persona was meant to portray. 

Mort is not a VB developer, Elvis is not a C# developer, and Einstein is not a C++ developer.  Sure, the personas use these analogies because they do fairly closely resemble a large stereotypical audience, but it doesn’t “fit” to anyone. Then again, no description fits more than one developer.  These personas don’t drive features and they don’t do anything but serve as reminders that we have different types of developers who need different types of features, documentation, and applications.  Don’t think this is true?  Ask the average VB developer what a thread is and they may get the “word for word” answer, but a large part of that audience never has wanted to understand the intricate details of thread local storage, differences between the stack and the heap and why those are important in the context of application development.  Does that mean that all VB developers don’t care about threading? NO. Once again, there is plenty of evidence that einsteins exist in the VB community as well  — “Einsteins” meaning people who want detail!  Mort behaviors exist in the C++ community as well. I’m one of the people have have a mort mentality with C++. I know so little about C++ I’m amazed I’m allowed to breath the same air as the folks here at Microsoft.  That, indeed, is the Mort side of me.

Several people have asked “Why are there no definitions for these from Microsoft.” And they are hopping mad about it!  Quite frankly, its because the personas were not meant to be public information.  They were used to help mold and categorize functionality internally, and nothing more.  Because of that, the actual documents for these personas are not available externally.   If they were, they would also likely be taken out of context.

That said, this won’t be the last discussion about these fictional characters.  I imagine we’ll be hearing about them for some time.  Just remember to take everything you hear with a grain of salt, and if you think the way we construct software is wrong, by all means RESPOND!  Speak up, tell us how you would approach it. Better yet, apply to the team that interests you most and come implement those changes yourself! 

Free Training CDs from AppDev

AppDev is apparently giving away free training CDs.  I have not personally seen the training, but free is not typically a trait in a product that I hate, so AppDev already “has me at hello” with this offer!
http://www.appdev.com/promo.asp?page=SN00009

What’s wrong with MSDN documentation?

A large part of my job at Microsoft is to provide developers with the knowledge they need to make use of the IIS 7.0 SDK.  While the information about IIS 7.0 is not out yet for review, there are plenty of docs provided in other areas, such as the .NET Framework SDK and ASP.NET sections, which can be used as a benchmark.   In order to make the IIS 7.0 SDK a success, we must do more than just put out a good product. We must also describe how to use it properly or it just will not be used.  So I pose a question to anyone who has ever looked something up in MSDN, what is wrong with the documentation you have used so far?  What is good about it?  What would you like to see to a greater/lesser extent in our documentation?   Feel free to flame us, or praise us.  If you have suggestions, comments, or ideas, feel free to express them here.

Your feedback will help me understand how to increase your satisfaction with MSDN and Microsoft in general.

Or better yet, if you have some desire to play with new technology that isn’t documented yet and provide that content to a worldwide audience, why not apply

Live from Redmond

A little over a year ago, I emailed Steve Ballmer at Microsoft directly about an idea to make a digital, online user group community. I wanted to be able to let user groups from across the country make their monthly presentations available via the web. Rather than being limited to what is available in local communities across the country, I thought it would be very beneficial to be able to watch any presentation at any user group on any given month: Community-created web-casts if you will. I was suprised to hear a response from Mr Ballmer direclty. I still have the email in my inbox because it reminds me just how much Microsoft DOES care — even the biggest guys in the company will handle customer-related issues. Mr Ballmer thought it was a great idea and gave me some contacts to discuss with INETA as well as several other groups. To be honest, I got way too involved with other things and sadly never followed up. Apparently, the idea wasn’t lost on the folks at INETA or MS though. Brad Abrams has just reported that this dream is close to a reality in a new offering called “Live From Redmond.”. Although it is not quite the community I was envisioning, it does at least make many user-group meetings available via live meeting. It’s a very exciting concept and I hope you’ll take advantage of it.

Threading issue with VB.NET Default Instances

Here is an issue that showed up for one of our customers in the Microsoft forums for Visual Basic .NET.  It is NOT the typical ”cross-threaded UI update” issue that you might think. No, this one, IMO, is slightly harder to catch since no exception is thrown to let you know something has gone wrong.

One of the many new features of Visual Basic .NET are default form instances.  As the feature lists explain, a default form instance prevents you from having to “new up” an instance of a form before acting on it.  So, instead of using:

Dim frm As New frmMain
frm.Show()

I can simply use:

frmMain.Show()

This is handy, particularly for the folks coming from VB6 who are used to forms that behave in this manner.  However, default instances present a very interesting problem.

Take, for instance, a project that I create with a form (named frmMain) and a module (named BackgroundMethods.vb) .  For the form, I have simply added a multi-line text box (named txtOutput) and a button control (named button1). 

Here is the code I have in my project:

[frmMain]

Imports System.Threading
Public Class frmMain
  Private Sub Button1_Click( ByVal sender As System.Object, _
                             ByVal e As System.EventArgs) _
                             Handles Button1.Click
     Dim t As Thread = New Thread(AddressOf GetData)
     t.Start()
     txtOutput.Text &= "Updates complete"
     ' Break after the call above to read the value
     ' in txtOutput.Text
  End Sub
End Class

[BackgroundMethods.vb]

Imports System.Threading
Module BackgroundMethods
   Public Sub GetData()
      WaitForData("Message 1")
      WaitForData("Message 2")
   End Sub
   Public Sub WaitForData(ByVal strMessage As String)
      ' Presumably this method would actually be
      ' waiting for data from a network connection,<
      ' serial port, or other source
      Thread.Sleep(2000)
      My.Forms.frmMain.txtOutput.Text &= (vbCrLf & Now().ToShortTimeString() & _
      vbTab & strMessage)
      ' Break after the call above to read the value
      ' in My.Forms.frmMain.txtOutput.Text
   End Sub
End Module

So the simple example is that I have a button which, when clicked, will execute the “GetData” method asynchronously in a module.  That method is going to call the WaitForData method that updates the UI with two different messages (“message1” and “message2”).  WaitForData is supposed to simulate a long-running process, so I threw in the typical “thread.sleep” call to make this illusion. 

If you run this code, you will notice that no exceptions are thrown, but the UI for your form is also not updated.  Why is this?  You would have at least expected a cross-thread exception, right?

In any other managed language, this likely wouldn’t happen — namely because the “My” application is specific to VB, as well as default form instances!  In C#, if I try to update the UI from another thread, I would get an exception stating: “Cross-thread operation not valid: Control ‘txtSerialIn’ accessed from a thread other than the thread it was created on.” which could be solved by using the “Invoke” method.   

The issue with Visual Basic, in this instance, is that the default form instances are thread-specific.  So, when I try to access the form using My.Forms.frmMain from within the worker thread, a NEW default instance is created under the covers.  My calls to update the text box are then executed on the NEW instance of the form which resides in the same thread as the call to update it — hence it doesn’t throw a cross-thread exception. In the mind of the VB program, the request to update the textbox occurred without an error.  When the worker thread dies, the second instance of the form (which was never displayed) is now a candidate for garbage collection.  Meanwhile, back on the original thread and original form the textbox is left blank.  You can validate this by running the program (I have attached sample code to this blog post) and setting breakpoints on the textbox update line in the WaitForData method, and in the last line of the button1_click event handler of the code.  You will notice that after the second call to WaitForData (before the debugger exits the Sub) that My.Forms.frmMain.txtOutput.Text has been properly set to the value you expected.  However, remember that this is on a second background instance of the form, not the original one you expected.  Once the debugger hits the last line in the button1_click event handler, read the value of txtOutput.Text and realize THAT instance of the textbox was never updated.

So what is the solution?

First off, you still have to use the “Invoke solution” that is often times bandied about in threading discussions.  To do this in VB.NET (and particularly in our solution), do the following:

1. Add the following code to the top of your BackgroundMethods.vb file:

Delegate Sub UpdateTextHandler(ByVal strMessage As String

This allows you to create a delegate that can be invoked on the UI.

2. Add the following method to your frmMain file:

Public Sub UpdateTextMethod(ByVal strMessage As String)
   txtOutput.Text &= (vbCrLf & Now().ToShortTimeString() & vbTab & strMessage)
End Sub

This creates the method that will actually be executed via your delegate from the worker thread.

3. Change your WaitForData method as follows:

Public Sub WaitForData(ByVal strMessage As String)
    ' Presumably this method would actually be
    ' waiting for data from a network connection
    ' serial port, or other source
    Thread.Sleep(2000)
    Dim f As frmMain = My.Application.OpenForms("frmMain")
    f.Invoke(New UpdateTextHandler(AddressOf f.UpdateTextMethod), _
             New Object() {strMessage})
End Sub

This method now uses the form’s “Invoke” method (actually defined in Control) to execute UpdateTextMethod on the original form via the UpdateTextHandler delegate.

So what is happening is that your new thread is getting an instance to the existing frmMain instance by going through the OpenForms call.  Once I have that instance, I can invoke a delegate that points to the “UpdateTextMethod” of the existing form ( passing in the message in the object array ).  By invoking, I am able to get back on the UI’s thread and that call can execute any updates to the UI that it wishes.

Keep this in mind the next time you are not receiving errors and your UI isn’t getting updated how you would expect — particularly if you code communicates with the network, a serial device, or other device which communicates asynchronously.

Free Book: Microsoft VB.NET 2005 for Developers

Microsoft has a free book available for download for anyone who wants it.  Download the whole book, or download it chapter-by-chapter (8 in all).  Check it out at http://msdn.microsoft.com/vbasic/learning/introtovb2005/

Debugging Resources

It is my opinion that the difference between good developers and outstanding ones are the way that they debug applications. As such, I wanted to point you to some great debugging resources to get you well on your way to standing with the outstanding developers.

First off, there is the issue of understanding Windows to begin with that get a lot of people in trouble.  Many issues can be traced down to something that is “working as designed”. As such, I highly recommend the Microsoft Windows Internals book from MS Press.  This book will give you a great understanding of how Windows works.  Next, I would brush up on .NET debugging skills. To do so,  I recommend a .NET 2003 book, but most of the debugging skills still apply to 2005, so don’t be dismayed by the title: Debugging Applications for Microsoft .NET and Windows. Once again, John Robbins from Wintellect writes one awesome book.

Well, books are great, but what about free resources?  I have some of those too!

First, you can check out the windows debugging website at:
http://www.microsoft.com/whdc/devtools/debugging/default.mspx

Then there are a new series of webcasts that have just been released in the past week or so dealing with the very issue of debugging:

TechNet Webcast: Microsoft.com Operations Introduces Real World Debugging: Determining When You Have a Problem and Beginning the Initial Debugging (Level 300)

TechNet Webcast: Microsoft.com Operations Introduces Real World Debugging: Debugging CLR Internals

TechNet Webcast: Microsoft.com Operations Introduces Real World Debugging: Diagnosing Memory Leaks in ASP.NET Applications (Level 300)

TechNet Webcast: Microsoft.com Operations Introduces Real World Debugging: How to Tackle Problems in Dynamically Generated Assemblies (Level 300)

TechNet Webcast: Microsoft.com Operations Introduces Real World Debugging: Debugging Without the Debugger in IIS and ASP.NET (Level 300)

OK. So maybe I did not have a TON of debugging resources, but this is a great start for anyone who wants to make himself or herself into an outstanding SDE or SDET. So go work yourself into a “debugging guru”.  By the time you are done, hopefully John Robbins’ new book on debugging will be out.

202 VB.NET Samples

I just wanted to bring some attention to a resource that I think is extremely valuable for VB developers (and really, any .NET developer using any other language for that matter).  MSDN has a website with 101 Code Samples for VB.NET 2005.  There is also another website for those of you that haven’t upgraded to 2005 yet: 101 Code Samples for VB.NET 2003.  Both of these sites have very valuable sample code for doing things such as network programming, using regular expressions, executing transactions, handling/changing ACL’s on files, accessing data, performing bulk data transactions, executing asynchronous operations (one of my favorite topics), using ClickOnce, creating ASP.NET pages and more!  It is a great resource for those of you that just want to get it done and don’t want to waste a lot of time reading specifications on each class in System.Net or System.Data.  The samples are robust and provide some great starting points for anyone interested in the many topics available in these samples.  Check them out and let me know what you think!