I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008. So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps.
Because there are a lot of programmers that are going to be making this move, I thought I would share this information. I'm certain it will be very valuable to those who are faced with this task.
One of the first things to think about is whether you want to upgrade your VB6 program to a VB.NET 2008 Windows Forms application or a VB.NET 2008 Windows Presentation Foundation (WPF) application. If you are upgrading to VB.NET 2005, then you don't have the WPF option, but if you are upgrading to VB.NET 2008, then you do, and you may choose to use it.
Why would you want to upgrade a VB6 Windows Forms application to WPF? Because that is the future of Windows programming. Windows Forms have been using the same technology for more than 15 years, and are now at the very beginning of their sunset. Windows Forms applications use User32 (User before 32-bit) and GDI/GDI+ for gui rendering, but WPF applications use DirectX for gui rendering, which is much more powerful. Additionally, WPF uses Extensible Application Markup Language (XAML) to define the layout of the interface, and instantiate .NET objects.
By far, the easiest choice is to upgrade a VB6 Windows Forms application to a VB.NET 2008 Windows Forms application. Since Windows Forms are going to be around for quite a while, we'll take a look at that.
Here are some steps and tips for upgrading a VB6 Windows Forms application to a VB.NET 2008 Windows Forms Application:
1. Use VS 2008's Upgrade Wizard
2. Change the Target Framework
3. Delete the Upgrade Report
4. Correct all errors
5. Update Code
6. Add API's to increase DoEvents performance.
"Upgrade failed: General error accessing file 'C'"
"Unable to read the project file..."
If you have a problem upgrading a VB6 application to VB.NET, then you have 3 options:
1. Install Visual Studio 2008 Service Pack 1 (SP1) and try it again.
2. Use the command-line version of the Wizard.
This is the same Upgrade engine that is used by the VS IDE Upgrade Wizard, but for some reason it worked every time a VB6 program crashed during upgrade. Here's how:
- Create a folder on the C:\ with a short name (like "Upgrade")
- Copy and paste the VB6 Project files into this folder.
- Open a Visual Studio Command Prompt
(Windows XP: Start button > All Programs > Microsoft Visual Studio 2008 >
Visual Studio Tools > Visual Studio 2008 Command Prompt)
- Make sure you are in the VB directory (mine opened to VC)
- Type "cd..", press Enter to move up 1 directory.
- Type "cd VB", press Enter to change directory.
- Go to the VBUpgrade directory
- Type "cd VBUpgrade", press Enter.
- Run the command-line version of the Upgrade Wizard:
- Include "VBUpgrade.exe"
- Include the input project path <filename>
- Include the new folder the VB.NET project will be created in (Output directory)
Structure:
VBUpgrade.exe <filename> /Out <directory>
Example:
VBUpgrade.exe "C:\Upgrade\Project1.vbp" /Out "C:\Upgrade\VB Upgrade"
- Press the Enter key after you type the above, and the Upgrade should begin.
3. If the command-line version of the Upgrade Wizard does not work for you, try
contacting John Hart at Microsoft (John.Hart@microsoft.com),
Dim myString As String = "Go ahead and search for this string"
- Instr - Instead of using the Instr() method to search a string, use the IndexOf() method.
Instr(myString, "search for this string") 'Old waymyString.IndexOf("search for this string") 'New way
- Mid - Instead of using the Mid() method to get a portion of a string, use the SubString() method.Mid(myString, 14) 'Old waymyString.SubString(13) 'New way
- Trim - Instead of using the Trim(), LTrim() and RTrim(), use .Trim(), .TrimStart(), .TrimEnd()Trim(myString), LTrim(myString), RTrim(myString) 'Old waymyString.Trim(), myString.TrimStart(), mystring.TrimEnd() 'New way
- Len - Instead of using the Len() method, use .Length() to get the length of a string.Len(myString) 'Old waymyString.Length() 'New way
- Replace the "And" operator with "AndAlso". (Do this in any non-bitwise comparison).If 1 = 1 And 2 = 2 And 3 = 3 Then 'And Old wayIf 1 = 1 AndAlso 2 = 2 AndAlso 3 = 3 Then 'And New way
- Replace the "Or" operator with "OrElse". (Do this in any non-bitwise comparison.)If 1 = 1 Or 2 = 2 Or 3 = 3 Then 'Or old wayIf 1 = 1 OrElse 2 = 2 OrElse 3 = 3 Then 'Or new way
- Replace ALL VB6 File I/O classes with the new .NET File I/O Classes. They are faster than VB6's so make sure you use them!Dim myFile As String = "C:\Temp\myfile.txt"Dim instring As String = String.Empty'** VB6 File I/O:FileOpen(1, myFile, OpenMode.Input)Do Until EOF(1) instring = LineInput(1) 'Read 1 line from a fileLoopFileClose(1)'** VB.Net File I/O:Dim reader As New System.IO.StreamReader(myF<wbr />ile)Do Until reader.EndOfStream = True instring = reader.ReadLine() 'Read 1 line from a fileLoopreader.Close()reader.Dispose()
Module Do_Events Friend Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Integer, _ ByVal nPriority As Integer) As Integer Friend Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Integer, _ ByVal dwPriorityClass As Integer) As Integer Friend Declare Function GetCurrentThread Lib "kernel32" () As Integer Friend Declare Function GetCurrentProcess Lib "kernel32" () As Integer Friend Const THREAD_PRIORITY_HIGHEST As Short = 2 Friend Const HIGH_PRIORITY_CLASS As Integer = &H80End Module
3. Add 2 lines of code before the intensive processing begins to set the thread priority:SetThreadPriority(GetCurre<wbr />ntThread, THREAD_PRIORITY_HIGHEST)SetPriorityClass(GetCurren<wbr />tProcess, HIGH_PRIORITY_CLASS)
4. Use DoEvents() sparingly! Calling DoEvents() has a big performance hit, so use it sparingly.Dim iLoops As Integer = 0Do Until iLoops = 10000'Calling DoEvents() every 500 loops will greatly increase application performanceIf iLoops Mod 500 = 0 Then DoEvents() iLoops += 1 'Add 1 to iLoopsLoop
5. Create a specific Sub routine for updating the controls on your Form, and call the Sub whenever you want to update the form. Private Sub UpdateForm() 'Update all controls here: progressBar1.Value += 1 'Update ProgressBar label1.Text = "Processing..." 'Update Labels, TextBoxes, etc... Application.DoEvents() 'Call DoEvents() so the form can refresh the changes to the controlsEnd Sub
If you follow the steps and tips included in this article, then your upgrade should go pretty smoothly, and your application should perform quite a bit faster in VB.NET than it did in VB6.
Good luck!
VBRocks