Why Choose vb.net? Repost

Source: Internet
Author: User
Tags command line count current time error handling soap new features web services visual studio
For. NET, people are more focused than Web applications. VB's first commitment to provide additional support for console programs and Windows services. Because of this, Rob MacDonald back to ask: what can you do with vb.net? In other words, VB. is net suitable for you?
So far, many software companies, including Microsoft, have compressed their products into one installation package for developers. We have been enduring the headaches of configuration and version matching issues. We have racked our brains for products in real-world situations. In order to test, we also spend a lot of time to set up a clean machine. For the next few years, the vast majority of software vendors and independent software development Studios will be out of this job for the simple reason that this is great.

For example, when Microsoft Office XP is in the market, you can either choose to buy at the price of the original package or pay a small license fee to rent for a year. This is part of the shift in the way that software, like products, is transformed into leased services. Eventually, I think you don't have to install anything in your PC (or PDA, cell phone, fridge, whatever). You just have to find the server that holds the software you need through a network that's always online. In some areas, this is already commonplace (WEB e-mail services are an excellent example). However, when most software can be quickly obtained in such a way, is it appropriate for developers like us?

So, to some extent, I think Microsoft's introduction of vb.net is an attempt to help us developers prepare for these upcoming service-centric paradigms. Web services have received so much attention because, through these technologies (HTTP,XML,SOAP), any operating system (including Windows, Linux, IBM, Palm) will be able to produce and deliver their services in such a way. This, of course, partly explains why Http,xml,soap are so simple. Because the computer itself must "read" them.

Of course, not all applications can be obtained through a service based approach. Vb. NET provides some real new features for the completion of some new and better applications on the desktop. For example, the "beautiful" forms used in VB6 only apply to VB, while the. NET, new Windows Forms are available not only to other. NET languages, but also to some significant innovations. I am particularly pleased to see that its support for window scaling, which I used to write, now I simply click on the mouse can set up the window scaling rules.

Console program

Vb. NET, one of the biggest benefits is that it allows VB developers to do something that was never done before. such as class inheritance and structured error handling. Another feature that keeps us waiting for a long time is that it can be used to write applications that interact with the user through the command line. Vb. NET makes this work easier, while preserving all. NET's functions.

In Visual Studio.NET, you can create a new project, called the console Program (console application). In fact, you may not be able to build vb.net applications through Visual Studio at all. All you really need is the. NET Framework SDK and Notepad, because the VB compiler, as a completely separate product, has been detached from Visual Studio. (In fact, whether you are compiling VB programs or generating source code for a defined dataset automatically, these Visual Studio.NET appear to be magical features that are implemented through the tools that are included with the. NET framework.) Visual Studio.NET just integrates them into an easy-to-use development environment. They are available as stand-alone command-line software. )

Let's use the command line to create a command line program. Here are some of the text I entered in Notepad and save it as a file "Speaktome.vb":

Imports System
Imports Microsoft.VisualBasic
Module Module1
Sub Main ()
Dim I, count as Integer
Try
Count = CInt (Command ())
For i = 1 to Count
Console.WriteLine ("Hello")
Next
Catch
Console.WriteLine ("Invalid Argument")
End Try
End Sub
End Module

This code runs several times according to the count value, and outputs "Hello" each time the text flows according to the standard output. The default is a command line control program. The program initializes the count variable by calling the command () function, which replaces the global variable command that we are familiar with in VB6 to return all the text that appears in the command line after the program name.

Although the code is fairly straightforward, we still need some annotations. First of all, we noticed that in the beginning it declared a reference to two namespaces using imports. The namespace system contains the console class, while namespace Microsoft.VisualBasic contains command functions, including many new features. When you build a vb.net project in Visual Studio, these namespaces are automatically imported, but when we use the compiler directly, we need to add them manually. And notice the way I declare my two variables. In VB6, a dim declaration like this would create a variant, an integer, but in the. NET. This traditional VB way completely disappears, I got the two integers I expected.

Vb. The net compiler can handle different parameters, but when I just pass the. vb file as a single parameter to it, it compiles a EXE file with the same name as it. Figure 1 (http://msdn.microsoft.com/library/en-us/dnvbdev01/html/rob0301fig1.jpg) shows the control session when I compile the program. (Compiler called "VBC")
The compiler for the command line makes the automated compilation process easier. The ability to write a console application, for VB, although insignificant but indeed a valuable extension.

Windows Services

Another new type of application is the Windows service. In addition to those bold VB developers, this may be beyond the other people's vision. Typically, this is a long-running program that does not need to rely on a logged-on user or client program to keep it running. They have no user interface of their own and can run in their own unique security level and session context. The Windows service paradigm that we are familiar with includes a print pool to a SQL Server and its Distributed transaction Collaboration (DTC).

Services can only run under NT and 2000, and they provide a specialized management interface through Microsoft Management Console (MMC). You can imagine that writing a service involves a lot of content, and I can't spend too much space here, but I can tell you how to build a simple sample.

There is a lot of information in the. NET beta1 online Help-I'm using the Beta1 version when I write this column-if you're going to build an operable Windows service, you should take a closer look.
Let me introduce the Windows service prototype that I will build to record the number of processes running on the specified machine. Yes, I know I can use Perfmon to do this, but I just pretend that I want to extend my service so that it can compute some statistical functionality and write them to the database, and maybe send mail to me when I get over a certain threshold.

I started by building a new Visual Studio.NET application, choosing VB as my development language, and the project type is Windows services. This operation automatically creates a new class for me, which is inherited. NET built the System.ServiceProcess.ServiceBase class. At the same time it provides me with a visual designer, a graphical tool for rapid development, especially for projects that do not have a user interface of their own. I can click on this designer to set the name of my service (I'm named "Process Tracking"). Then I dragged two components from the toolbar to the designer-a PerformanceCounter component, a Timer component. I can set them up through the property bar and feel like they're the kind of visual controls, even though I don't need a form to place them. I configure the timer to activate every 60 seconds, and PerformanceCounter is used to monitor the number of processes running on my machine. You can see the property settings for the designer and PerformanceCounter components in Figure 2 (http://msdn.microsoft.com/library/en-us/dnvbdev01/html/rob0301fig2.jpg). (I can switch between the designer and the Code window, just as I did a form.) )
Visual Studio generates the default sub Main, which is used to handle the run of the start service. Because your code is inherited from the ServiceBase class, you can get some standard events in the service lifecycle. These events are critical in the life cycle of the service. They are:

OnStart
OnStop
OnPause
OnContinue
OnShutdown

These codes are very much like the load/unload/activate/deactivate events in the VB6 form. Here's my code for OnStart and OnStop design:
Protected Overrides Sub OnStart (ByVal args () _
As String)
Dim FS as New FileStream ("C:\log.txt", _
FileMode.OpenOrCreate, FileAccess.Write)
Dim SW as New StreamWriter (FS)
Sw. Basestream.seek (0, Seekorigin.end)
Sw. WriteLine ("Service started:" + CSTR (now))
Sw. Close ()
End Sub

Protected Overrides Sub OnStop ()
Dim FS as New FileStream ("C:\log.txt", _
FileMode.OpenOrCreate, FileAccess.Write)
Dim SW as New StreamWriter (FS)
Sw. Basestream.seek (0, Seekorigin.end)
Sw. WriteLine ("Service Stopped:" + CStr (now))
Sw. Close ()
End Sub

These routines open a Log.txt file and write a simple message with a timestamp. The StreamWriter class provides us with an efficient way to create a character stream, while the FileStream class allows the StreamWriter class to be associated with a physical file. Closing the StreamWriter also closes the file that he is connected to.

Public Sub Timer1_Tick (ByVal sender as Object, _
ByVal e as System.EventArgs) Handles Timer1.tick
Dim LogRecord as String = CStr (now ()) + ""
Dim FS as New FileStream ("C:\log.txt", _
FileMode.OpenOrCreate, FileAccess.Write)
Dim SW as New StreamWriter (FS)
LogRecord + + PerformanceCounter1.NextValue.ToString
Sw. Basestream.seek (0, Seekorigin.end)
Sw. WriteLine (LogRecord)
Sw. Close ()
End Sub

As you can see, the code initializes a string with the current time and joins it with the value read from PerformanceCounter, which returns the number of processes currently running on my machine. The string is then written to the end of the log file.

Turning this project into a service requires more than simply compiling it. First, you need to add a few additional installation code so that the program will work properly after it has been installed as a service. Luckily, the visual designer provides a menu option ("Add Installer") that automatically generates this code. Then, after you compile the project, you need to run InstallUtil.exe (which is one of the tools of the. NET SDK) on the command line to process the compiled works, so that the service is installed on your system and the necessary registry entries are established.

Once the service is installed, we need to start it-it's not difficult to use the service management tool. Different systems, there are different ways to access the tool. In Windows Professional, you can get a management tool in the Control Panel. In Figure 3 (http://msdn.microsoft.com/library/en-us/dnvbdev01/html/rob0301fig3.jpg), you can see that the service has been manually started. You can also see that it is configured to start automatically, which means that the next time I start my system, the service will start running, even before I log on to the system.

Figure 4 (http://msdn.microsoft.com/library/en-us/dnvbdev01/html/rob0301fig4.jpg) shows a partial log of this service generated on my system. During that time, I set up some processes, logged out, logged in again, and restarted the machine (which also caused the service to reboot).

. NET project type

I have highlighted two types of specific vb.net projects. The types of engineering that VB developers can implement are listed in Table 1 (in any case, in beta1) with some and language-independent engineering types, such as: installation, configuration engineering, database engineering, and analytical engineering.


Project type description

Windows application builds applications from the Windows Forms-based user interface.
The class Library generates class libraries for other applications.
Windows control Library in windows-based controls.
The Web application uses a static or dynamic Web page to generate applications as a user interface.
The Web Service generates web Services from other applications.
The Web control Library generates controls in a Web application.
The Console application is used to build command line applications.
Windows service for Windows generation.

One of the reasons to showcase the console and service applications is to emphasize that vb.net does not force us to become N-tier Web developers. In fact, Microsoft invests heavily to provide support for traditional desktop applications. Although. NET means learning a lot of new things, you can think of vb.net as an ambitious big change-it clears some of VB's drawbacks and pushes it to its peak with backward compatibility.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.