vb3.0 升级vb6.0_将VB6升级到VB.NET(性能改进)

本文详细介绍了如何将VB6应用程序升级到VB.NET 2008,包括使用Visual Studio 2008升级向导、更改目标框架、错误修正、代码更新和提高DoEvents性能的技巧。通过这些步骤,可以实现性能显著提升,使VB.NET应用比VB6运行更快。
摘要由CSDN通过智能技术生成

vb3.0 升级vb6.0

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.

我目前在一家公司工作,我必须将50多个VB6程序升级到VB.NET2008。到目前为止,我已经完成了一半,而且我学到了许多技巧,可以极大地改善VB的性能。 NET应用程序。

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.  

首先要考虑的事情之一是是否要将VB6程序升级到VB.NET 2008 Windows Forms应用程序或VB.NET 2008 Windows Presentation Foundation(WPF)应用程序。 如果要升级到VB.NET 2005,则没有WPF选项,但是如果要升级到VB.NET 2008,则可以,并且可以选择使用它。

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.

您为什么要将VB6 Windows窗体应用程序升级到WPF? 因为那是Windows编程的未来。 Windows Forms使用相同的技术已有15多年了,现在才刚刚起步。 Windows Forms应用程序使用User32(32位之前的用户)和GDI / GDI +进行gui渲染,但是WPF应用程序使用DirectX进行gui渲染,功能更强大。 此外,WPF使用可扩展应用程序标记语言(XAML)定义接口的布局,并实例化.NET对象。

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.

到目前为止,最简单的选择是将VB6 Windows窗体应用程序升级到VB.NET 2008 Windows窗体应用程序。 由于Windows窗体将存在很长一段时间,因此我们将对其进行研究。

Here are some steps and tips for upgrading a VB6 Windows Forms application to a VB.NET 2008 Windows

以下是将VB6 Windows窗体应用程序升级到VB.NET 2008 Windows的一些步骤和技巧。

Forms Application:

表格申请:

     Use VS 2008's Upgrade Wizard

使用VS 2008的升级向导

     Change the Target Framework

更改目标框架

     Delete the Upgrade Report

删除升级报告

     Correct all errors

更正所有错误

     Update Code

更新代码

     Add API's to increase DoEvents performance.

添加API以提高DoEvents性能。

1.使用Visual Studio 2008升级向导 (1. Using the Visual Studio 2008 Upgrade Wizard)

     Open Visual Studio 2008

打开Visual Studio 2008

     From the File menu, select Open | Project/Solution

从文件菜单中,选择打开|。 项目/解决方案

         -  Navigate to the VB6 project that you want to upgrade, and select it.

-导航到要升级的VB6项目,然后选择它。

         -  The Upgrade Wizard will start automatically.

-升级向导将自动启动。

         -  Click "Next" through each window of the Wizard until the Wizard begins the conversion.

-在向导的每个窗口中单击“下一步”,直到向导开始转换。

     ** Upgrade Errors:

**升级错误:

     I have encountered some VB6 projects that did not complete the Upgrade Wizard, and would not

我遇到一些VB6项目没有完成升级向导,并且不会

     upgrade. A couple of the errors I received were:  "Upgrade failed:  General error accessing file 'C'"

升级。 我收到的几个错误是:“升级失败:访问文件'C'时出现一般错误”

     and "Unable to read the project file..."  

和“无法读取项目文件...”

     If you have a problem upgrading a VB6 application to VB.NET, then you have 3 options:

如果将VB6应用程序升级到VB.NET时遇到问题,则有3个选项:

          1.  Install Visual Studio 2008 Service Pack 1 (SP1) and try it again.

1.安装Visual Studio 2008 Service Pack 1(SP1),然后重试。

          2.  Use the command-line version of the Wizard.  

2.使用向导的命令行版本。

               This is the same Upgrade engine that is used by the VS IDE Upgrade Wizard, but for some reason

这是VS IDE升级向导使用的同一升级引擎,但是出于某些原因

               it worked every time a VB6 program crashed during upgrade.  Here's how:

每次在升级过程中VB6程序崩溃时,它都起作用。 这是如何做:

                   -  Create a folder on the C:\ with a short name (like "Upgrade")

-在C:\上创建一个带有短名称的文件夹(例如“升级”)

                   -  Copy and paste the VB6 Project files into this folder.

-将VB6项目文件复制并粘贴到此文件夹中。

                   -  Open a Visual Studio Command Prompt

-打开Visual Studio命令提示符

                       (Windows XP:  Start button > All Programs > Microsoft Visual Studio 2008 > 

(Windows XP:开始按钮>所有程序> Microsoft Visual Studio 2008>

                       Visual Studio Tools > Visual Studio 2008 Command Prompt)

Visual Studio工具> Visual Studio 2008命令提示符)

                            -  Make sure you are in the VB directory (mine opened to VC)

-确保您在VB目录中(我的VC已打开)

                                     -  Type "cd..", press Enter to move up 1 directory.

-键入“ cd ..”,然后按Enter键向上移动1个目录。

                                     -  Type "cd VB", press Enter to change directory.

-键入“ cd VB”,按Enter更改目录。

                            -  Go to the VBUpgrade directory

-转到VBUpgrade目录

                                     -  Type "cd VBUpgrade", press Enter.

-键入“ cd VBUpgrade”,然后按Enter。

                            -  Run the command-line version of the Upgrade Wizard:

-运行升级向导的命令行版本:

                                     -  Include "VBUpgrade.exe"

-包括“ VBUpgrade.exe”

                                     -  Include the input project path <filename>

-包括输入项目路径<文件名>

                                     -  Include the new folder the VB.NET project will be created in (Output directory)

-包括将在(输出目录)中创建VB.NET项目的新文件夹

                                     Structure:

结构体:

                                     VBUpgrade.exe <filename> /Out <directory>

VBUpgrade.exe <文件名> / Out <目录>

                                     Example:

例:

                                     VBUpgrade.exe "C:\Upgrade\Project1.vbp" /Out "C:\Upgrade\VB Upgrade"                                      

VBUpgrade.exe“ C:\ Upgrade \ Project1.vbp” / Out“ C:\ Upgrade \ VB升级”

                                     -  Press the Enter key after you type the above, and the Upgrade should begin.

-输入上述内容后,按Enter键,应该开始升级。

          3.  If the command-line version of the Upgrade Wizard does not work for you, try

3.如果升级向导的命令行版本不适合您,请尝试

               contacting John Hart at Microsoft (John.Hart@microsoft.com), he may be able to help.

与Microsoft的John Hart联系(John.Hart@microsoft.com), 他也许可以提供帮助。

2.更改目标框架 (2. Change the Target Framework)

     By default the Target Framework will be set to ".Net Framework 2.0".  You can use this if you want to.

默认情况下,目标框架将设置为“ .Net Framework 2.0”。 您可以根据需要使用它。

     I changed it to ".Net Framework 3.5".  If you desire to do so, here's how:

我将其更改为“ .Net Framework 3.5”。 如果您希望这样做,请按照以下步骤操作:

          -  Click on the Project Menu | Properties

-单击项目菜单| 物产

          -  Click the Compile tab

-点击编译标签

          -  Click the "Advanced Compile Options..." button at the bottom of the tab page

-点击标签页底部的“高级编译选项...”按钮

          -  Change the "Target framework" to ".Net Framework 3.5"

-将“目标框架”更改为“ .Net Framework 3.5”

          -  Click "OK", then "Yes"

-点击“确定”,然后点击“是”

3.删除升级报告 (3. Delete the Upgrade Report)

     When you perform an Upgrade using the Upgrade Wizard, an Upgrade Report is automatically generated.

使用升级向导执行升级时,会自动生成一个升级报告。

     If you would like to look at the report, then Open the Solution Explorer in Visual Studio (View menu |

如果要查看报告,请在Visual Studio中打开“解决方案资源管理器”(“视图”菜单|

     Solution Explorer), and double-click "_UpgradeReport.htm".  I personally haven't used the report, so I

解决方案资源管理器),然后双击“ _UpgradeReport.htm”。 我个人没有使用过报告,所以我

     delete it (right-click, Delete in Solution Explorer).

删除它(右键单击,在解决方案资源管理器中删除)。

          - If you decide to delete the report, you'll also need to open Windows Explorer and navigate to your

-如果您决定删除报告,则还需要打开Windows资源管理器并导航到

            project folder, then delete the "_UpgradeReport_Files" folder.

项目文件夹,然后删除“ _UpgradeReport_Files”文件夹。

            Example:  delete "C:\Upgrade\VB Upgrade\_UpgradeReport_Files"

示例:删除“ C:\ Upgrade \ VB Upgrade \ _UpgradeReport_Fil es”

4.纠错 (4. Error Correction)

     Once you upgrade your VB6 Windows Forms application to .NET, you will have lots of errors!  To see a

将VB6 Windows窗体应用程序升级到.NET后,将出现很多错误! 看一个

     list of errors, open the "Error List" (View menu | Error List).

错误列表,打开“错误列表”(“查看”菜单|“错误列表”)。

     To actually go to an error, double-click an error from the Error List.  Visual Studio will automatically take

要实际处理错误,请从“错误列表”中双击一个错误。 Visual Studio会自动将

     you to the line of code where the error occurs.  

您转到发生错误的代码行。

     Above the line of code where the error occurs, you will notice an "Upgrade Warning".  This warning

在发生错误的代码行上方,您会注意到“升级警告”。 这个警告

     describes the error, and provides a fairly helpful link that can help you get more information about the

描述错误,并提供一个相当有用的链接,可以帮助您获取有关

     error, plus steps you can take to fix the error.  To use the link, hold down the control button and left-

错误,以及可以采取的纠正错误的步骤。 要使用链接,请按住控制按钮,然后向左

     click it with your mouse.

用鼠标单击它。

          For example, if you had a CommonDialog control on your VB6 form, then you will receive an error

例如,如果您的VB6表单上有一个CommonDialog控件,那么您将收到一个错误

          informing you that the CommonDialog was not upgraded.  The helpful link will provide links to new

通知您CommonDialog尚未升级。 有用的链接将提供新的链接

          controls that replace the CommonDialog, such as "OpenFileDialog", "SaveFileDialog", etc.

替代CommonDialog的控件,例如“ OpenFileDialog”,“ SaveFileDialog”等。

     Correct all of the errors in the project before continuing.

在继续之前,请更正项目中的所有错误。

5.更新代码 (5. Update Code)

     VB.Net 2008 continues to support many VB6 methods.  HOWEVER, they are actually SLOWER than their

VB.Net 2008继续支持许多VB6方法。 但是,它们实际上比它们慢

     VB.NET counterparts, so it is very important to go through each line of code in your project, and replace

VB.NET对应项,因此遍历项目中的每一行代码并进行替换非常重要

     each VB6 method with it's .NET counterpart.

与.NET对应的每个VB6方法。

     VB6 code runs good in VB6, but VB6 code in VB.NET runs bad (very bad).  VB.NET code in VB.NET runs

VB6代码在VB6中运行良好,但是VB.NET中的VB6代码运行不良(非常糟糕)。 VB.NET中的VB.NET代码运行

     good (VERY good), much faster than VB6 code runs in VB6 (if that makes sense).

好(非常好),比在VB6中运行VB6代码要快得多(如果可以的话)。

     So here are some examples of how to replace VB6 methods with VB.NET counterparts:

因此,这里有一些示例,说明了如何用VB.NET对应项替换VB6方法:

         'TIP:  VB.Net strings are zero based, in other words, the first position of a string is 0.  In VB6, the

提示:VB.Net字符串是从零开始的,换句话说,字符串的第一个位置是0。在VB6中,

                 first position was 1.  This greatly affects how strings are parsed.

第一位置是1。这极大地影响了字符串的解析方式。

         Dim myString As String = "Go ahead and search for this string"

Dim myString As String =“继续搜索该字符串”

          -  Instr - Instead of using the Instr() method to search a string, use the IndexOf() method.

-Instr-使用IndexOf()方法代替使用Instr()方法搜索字符串。

               Old way:  Instr(myString, "search for this string")

旧方法:Instr(myString,“搜索此字符串”)

               New way:  myString.IndexOf("search for this string")

新方法:myString.IndexOf(“搜索此字符串”)

          -  Mid - Instead of using the Mid() method to get a portion of a string, use the SubString() method.

-Mid-代替使用Mid()方法来获取字符串的一部分,而应使用SubString()方法。

               Old way:  Mid(myString, 14)

旧方法:Mid(myString,14)

               New way:  myString.SubString(13)

新方法:myString.SubString(13)

          -  Trim - Instead of using the Trim(), LTrim() and RTrim(), use .Trim(), .TrimStart(), .TrimEnd()

-修剪-代替使用Trim(),LTrim()和RTrim(),而使用.Trim()、. TrimStart()、. TrimEnd()

               Old way:  Trim(myString), LTrim(myString), RTrim(myString)

旧方法:Trim(myString),LTrim(myString),RTrim(myString)

               New way:  myString.Trim(), myString.TrimStart(), mystring.TrimEnd()

新方法:myString.Trim(),myString.TrimStart(),mystring.TrimEnd()

          -  Len - Instead of using the Len() method, use .Length() to get the length of a string.

-Len-代替使用Len()方法,而使用.Length()来获取字符串的长度。

               Old way:  Len(myString)

旧方法:Len(myString)

               New way:  myString.Length()

新方法:myString.Length()

          -  Replace the "And" operator with "AndAlso", replace the "Or" operator with "OrElse".

-将“ And”运算符替换为“ AndAlso”,将“ Or”运算符替换为“ OrElse”。

             Do this in any non-bitwise comparison.

在任何非按位比较中执行此操作。

               And Old way:  If 1 = 1 And 2 = 2 And 3 = 3 Then

和旧的方式:如果1 = 1和2 = 2和3 = 3然后

               And New way:  If 1 = 1 AndAlso 2 = 2 AndAlso 3 = 3 Then

新方法:如果1 = 1 AndAlso 2 = 2 AndAlso 3 = 3然后

               Or Old way:  If 1 = 1 Or 2 = 2 Or 3 = 3 Then

或旧方法:如果1 = 1或2 = 2或3 = 3然后

               Or New way:  If 1 = 1 OrElse 2 = 2 OrElse 3 = 3 Then

或新方法:如果1 = 1 OrElse 2 = 2 OrElse 3 = 3然后

          -  Replace ALL VB6 File I/O classes with the new .NET File I/O Classes.  They are faster than VB6's

-用新的.NET文件I / O类替换所有VB6文件I / O类。 它们比VB6的要快

             so make sure you use them!

因此,请确保您使用它们!

              Dim myFile As String = "C:\Temp\myfile.txt"

Dim myFile As String =“ C:\ Temp \ myfile.txt”

              Dim instring As String = String.Empty

Dim instring As String = String.Empty

               ** VB6 File I/O:

** VB6文件I / O:

               FileOpen(1, myFile, OpenMode.Input)

FileOpen(1,myFile,OpenMode.Input)

               Do Unil EOF(1)

做Unil EOF(1)

                    instring = LineInput(1)  'Read 1 line from a file

instring = LineInput(1)'从文件中读取1行

               Loop

循环

               FileClose(1)

文件关闭(1)

               ** VB.Net File I/O:

** VB.Net文件I / O:

               Dim reader As New System.IO.StreamReader(myFile)

昏暗的阅读器作为新System.IO.StreamReader(myF ile)

               Do Until reader.EndOfStream = True

做直到reader.EndOfStream = True

                    instring = reader.ReadLine()

instring = reader.ReadLine()

               Loop

循环

               reader.Close()

reader.Close()

               reader.Dispose()

reader.Dispose()

6. DoEvents (6. DoEvents)

     As soon as I upgraded processor intensive VB6 applications to VB.NET, I noticed that the performance

将处理器密集型VB6应用程序升级到VB.NET后,我立即注意到性能

     was terrible!  While code upgrades are contributing factors, DoEvents is one of the biggest culprits!  VB6

太可怕了! 尽管代码升级是促成因素,但DoEvents是最大的罪魁祸首之一! VB6

     applications ran (in some cases) about 10 times slower when they were upgraded to VB.NET.  With a

升级到VB.NET时,应用程序的运行速度(某些情况下)慢了大约10倍。 用

     few tweaks, VB.NET application performance can be greatly improved, so that they run about 40% -

进行一些调整后,VB.NET应用程序的性能可以大大提高,从而使它们运行大约40%-

     50% FASTER than VB6...  

比VB6快50%...

     1.  Add a Module to your project, and name it something like "Do_Events"

1.在您的项目中添加一个模块,并将其命名为“ Do_Events”

     2.  Insert the following code into the module you added:

2.将以下代码插入您添加的模块中:

          Module Do_Events

模块Do_Events

               Friend Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Integer, _

朋友声明函数SetThreadPriority Lib“ kernel32”(ByVal hThread作为整数,_

                    ByVal nPriority As Integer) As Integer

ByVal nPriority As Integer)As Integer

               Friend Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Integer, _

朋友声明函数SetPriorityClass Lib“ kernel32”(ByVal hProcess为整数,_

                    ByVal dwPriorityClass As Integer) As Integer

ByVal dwPriorityClass As Integer)作为Integer

               Friend Declare Function GetCurrentThread Lib "kernel32" () As Integer

朋友声明函数GetCurrentThread Lib“ kernel32”()作为整数

               Friend Declare Function GetCurrentProcess Lib "kernel32" () As Integer

朋友声明函数GetCurrentProcess Lib“ kernel32”()作为整数

               Friend Const THREAD_PRIORITY_HIGHEST As Short = 2

朋友常量THREAD_PRIORITY_HIGHEST短= 2

               Friend Const HIGH_PRIORITY_CLASS As Integer = &H80

Friend Const HIGH_PRIORITY_CLASS作为整数=&H80

          End Module

终端模块

     3.  Add 2 lines of code before the intensive processing begins to set the thread priority:

3.在密集处理开始设置线程优先级之前,添加两行代码:

          SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_HIGHEST)

SetThreadPriority(GetCurre ntThread,THREAD_PRIORITY_HIGHEST)

          SetPriorityClass(GetCurrentProcess, HIGH_PRIORITY_CLASS)  

SetPriorityClass(GetCurren tProcess,HIGH_PRIORITY_CLASS)

     4.  Use DoEvents() sparingly!  Calling DoEvents() has a big performance hit, so use it sparingly.

4.谨慎使用DoEvents()! 调用DoEvents()会对性能产生重大影响,因此请谨慎使用。

          Dim iLoops As Integer = 0

Dim iLoops作为整数= 0

          Do Until iLoops = 10000

直到iLoops = 10000为止

               'Calling DoEvents() every 500 loops will greatly increase application performance

'每500个循环调用DoEvents()将大大提高应用程序性能

               If iLoops Mod 500 = 0 Then DoEvents()

如果iLoops Mod 500 = 0,则DoEvents()

               iLoops += 1   'Add 1 to iLoops

iLoops + = 1'向iLoops加1

          Loop

循环

     5.  Create a specific Sub routine for updating the controls on your Form, and call the Sub whenever you

5.创建一个特定的Sub例程以更新窗体上的控件,并在您每次调用Sub时

          want to update the form.  

想要更新表格。

          In the example above, you can substitue DoEvents() with the name of your update method

在上面的示例中,可以用更新方法的名称替换DoEvents()

          Example:

例:

          Private Sub UpdateForm()

私人子UpdateForm()

               'Update all controls here:

'在此处更新所有控件:

               progressBar1.Value += 1        'Update ProgressBar

progressBar1.Value + = 1'更新ProgressBar

               label1.Text = "Processing..."   'Update Labels, TextBoxes, etc...

label1.Text =“正在处理...”'更新标签,文本框等...

               Application.DoEvents()  'Call DoEvents() so the form can refresh the changes to the controls

Application.DoEvents()'调用DoEvents(),以便表单可以刷新对控件的更改

          End 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.  

如果您按照本文中包含的步骤和技巧进行操作,则升级应该会非常顺利,并且您的应用程序在VB.NET中的执行速度应比在VB6中的执行速度要快得多。

Good luck!  

祝好运!

VBRocks

VBRocks

翻译自: https://www.experts-exchange.com/articles/302/Upgrading-VB6-to-VB-NET-Performance-Improvements.html

vb3.0 升级vb6.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值