Using .NET Framework, Microsoft Visual Basic developers can create robust, which is difficult to write in the previous Visual Basic version. This article will discuss the benefits of use .NET Framework, and will include some functions, Visual Basic developers can use features with the framework, including multi-thread pool, Windows service, and file system monitoring.
Why use a framework?
There are several types of words framework (Framework). In this case, it refers to the basis for creating and running applications. Having such a foundation makes it easier to create an application, while using a consistent, simplified programming model.
As a Visual Basic 6.0 developer, you feel very satisfied with this programming language, which makes it easy to create a variety of applications. The Visual Basic language itself provides an inherent data type, such as Integer, Long, and String, as well as some of the most common functions such as string processing, and data type conversion. When your application becomes more complicated, you can use the WIN32 API to complete the functionality that the standard Visual Basic function is implemented - such as any registration key and value. In many cases, you can also use COM (Component Object Model, Component Object Model) Component library to extend the function of the application; the most obvious example is the ADO (ActiveX Data Objects) library, your application can use it data access.
Although Visual Basic is flexible, these different scalability mechanisms can be provided, but this flexibility still requires you to learn a few complex API architectures. You need to know how Win32 work, and how to call them in Visual Basic, this may be a task that is timely and easy to errors. You also need to know how to use a variety of COM components in Visual Basic, each COM component has a different object model.
Finally, when you use Win32 API, ADO, you may also use many other COM components, when you create your own Visual Basic application, you need to manage the deployment of these applications and their relevance. A typical Visual Basic application's relevance list is far more than Visual Basic runtime (Runtime); it must include all object libraries used by the application, such as ADO 2.6.
The idea behind the public frame is to solve these problems and make users create robust applications more easily without learning a variety of different API architectures, and does not need to deploy and process version issues of multiple object libraries.
What is .NET Framework
The term .NET Framework refers to a group of technologies that make up the development of Microsoft .NET platform development. The key technologies in this group are runtime and class libraries, as shown in Figure 1.
Figure 1. .NET Framework consists of .NET runtime and class library
The runtime is responsible for managing the code, providing services to it when executing, which is similar to the Runtime of Visual Basic 6.0. .NET programming language - including Visual Basic .NET, Microsoft Visual C #, C management extensions, and a variety of programming languages from different developers - various services and features through a group of public uniform classes.
The .NET unified class provides the basis for creating applications, regardless of what language you use. Whether you just simply connect a string or create a Windows service or multi-level network-based application, you have to use these unified classes.
The uniform class provides a consistent way for the functionality of the access platform. Once you have learned to use the class library, you will find that all tasks follow the same consistent architecture. To write your own app, you don't have to learn and master different API architectures. Due to the .NET Framework, deploy Visual Basic .NET application is easier. Unlike Visual Basic 6.0 applications, you don't need to configure various relevance, such as separate data access libraries, XML syntax analyzers, and network APIs because all of these features are part of the .NET Framework.
By creating your own apps on a unified, integrated frame, you can achieve the largest return spending time spending this framework, and you will have a strong application that is easily deployed and used.
.NET Framework and Visual Basic .NET
Visual Basic 6.0 is very important to simplify many public programming tasks. But simplify this layer means that you cannot use new operating system features before you have a package program that you can use, such as DirectX. As a Visual Basic developer, the most important benefit you get from .NET Framework is that the consistent programming model can be used directly and easily access the .NET platform. This means that you can use Visual Basic .NET creation difficult or impossible applications created using Visual Basic 6.0. As a Visual Basic 6.0 developer, you will now appreciate the same features and features that can be used with other platform languages. For example, in order to create a Windows service, you don't need to write it with Microsoft Visual C , you do not need to help hackers or assemblers. You can use Visual Basic .NET to complete this work using Visual Basic .NET. In order to give you some examples of using .NET FRAMWORK, we will discuss 4 common tasks that may need to be executed in your application: track and create Windows services with event records, multi-threads, file system monitoring and creation.
Track and event record
When you create a robust app, you must pay close attention to the diagnostics and troubleshooting mechanisms. Representatively, this includes writing processing to open an output target (event record or file) tracking component, writing tracking messages, and shutting down output targets. The text is then transferred to the record with the method of this component through its own code. You will spend all the time and energy on the creation tracking and recording subsystem, which will eventually contribute to the business problem, but this is necessary to create an application.
.NET Framework includes classes and other data types that make it easy to record tracking messages by providing you with a record infrastructure. Figure 2 shows the .NET Framework class for tracking.
Figure 2. Tracking tools used in .NET Framewok
Class is part of the SYSTEM.DIAGNOSTICS name. The TRACE class provides several ways of sharing. For example, the WRITE method allows you to record specific messages, and the ASSERT method allows you to record a message in the case where specific conditions are false. The TRACE class outputs the message to the object in the ListenerS collection. Each object in this collection belongs to a class inherited from the TraceListener. EventLogTraceListener writes the message to event records, and TextWritertraceListener is written to a text file. By default, an instance of DefaultTraceListener is added to the listeners collection of the Trace class.
In addition to the standard listener, you can implement your own tracking listeners. For example, you want to receive tracking output from applications running on a remote machine behind a firewall. You can write a tracking listener, send a tracking message to your server through HTTP. This will affect your application performance, but only affect performance when you enable tracing. Representative, you need to have the ability to include or remove tracking code in the compiled binaries. In order to do this in Visual Basic 6.0, you need to use compilation constants and include all tracking code in the #IF statement, which makes the code difficult to understand and maintain. With .NET Framework, you only need to set Trace compilation constants on or OFF status in the Project Properties dialog; you do not need to include tracking code in the #IF statement.
Another universal expected tracking feature is tracking horizontal settings, including different tracking settings -, for example, Severe, Error, Warning (Warning), and Information - These settings are controlled. You can control this using the registry value read when the tracking component is started. This is a completely built-in function for .NET Framework. You can set up a registration table value to control your current record level, for example, using Trace.Writeif and Trace.WritelineIf using Trace.WriteIf and Trace.WritelineIf only if the tracking level is set to Severe (severe).
Integrated tracking and recording features greatly enhance productivity, because you only need to use built-in features, focus on writing real application code.
Multi-threaded application
A very important feature of .NET Framework is that you can create multi-threaded applications using Visual Basic without using a third-party tool or unsupported Visual Basic skill. The multi-threaded support of .NET Framework is provided by classes and interfaces in the System.Thream Namespace, so all .NET languages can create and process threads in the same way. System.Threading.Thread is a core class that provides support for creating and controlling threads. To create a thread, you can create a new System.Threading.Thread object, pass the constructor to the Threadstart agent. This agent provides a method that this thread begins to execute. When you are ready to start this new thread, you can call Thread.Start () (see Listing 1).
When you start creating multi-threaded applications, you will soon recognize access to shared resources, such as shared class variables. The .NET Framework also includes several classes and data types, you can use them to synchronize the two threads performed.
In the easiest case, you have a shared variable that needs to be updated from different threads. To do this, you can use the System.Threading.Interlocked class. For example, you can increase or decrease the shared variable named NUM by writing interlocked.increment (num) or interlocked.Decrement (NUM). You can also use interlocked to set the variable to a particular value, or check if the two variables are equal. In addition to this simple condition, you can use the .NET Framework class to perform more complex thread synchronization, such as events and mutex synchronization - all from .NET Framework inside without having to use Win32 API. .
Imports system.io
'The namespace system.threading
'Contains the Thread Class
Imports system.threading
Module Module1
Private Count As Long
Sub main ()
'Create the Threadstart Delegate
DIM TSTART As Threadstart = New_
ThreadStart (Addressof StartCounting)
'Create the Thread
DIM T1 As Thread = New Thread (TStart)
Console.writeline ("Enter Q to Quit")
T1.Start () 'Start The Thread
While (console.read () <> ASC ("Q"))
'Repeat the loop untric the user enters Q
End while
T1.Stop () 'Tell Thread to Stop Processing
T1.join () 'Wait Until The Thread Finishes
End Sub
SUB StartCounting ()
DO
'use interlocked.increment in case
'Another Thread Is Accessing The Same Variable
Interlocked.Increment (count)
Console.writeline (_
"After incrementing count is: {0}", count)
Thread.sleep (200)
Loop
End Sub
End module
Listing 1. Create threads using Visual Basic .NET
You created a new thread and passed it to a ThreadStart agent. Then call Thread.Start () to launch this thread. You can troubleshoot this thread by calling thread.stop (), then call thread.join () Wait for it to complete the shutdown. A thread can use system.threading.interlocked to increment or decrease the variable.
In addition, .NET Framework provides a convenient mechanism to queue a work, and will be assigned to a thread in the thread pool. This is very useful in the server application that handles multiple concurrent work items or work requests. For example, for applications waiting for input files, then import them into the database, you may queuon each input file to process on a separate thread in the thread pool. System.Threading.ThreadPool class allows you to queue your work using shared QueueUserWorkItem methods. To do this before, you have to create and manage your thread pool. You need to work in infrastructure instead of solving a lot of time and effort on business issues.
File system monitoring
I have encountered some applications that need to wait and handle files in a particular directory - for example, apply data from files into the database. Data files can be downloaded from a large machine or transferred to an input directory, which imports them into the database. You don't have to regularly poll the directory to check if there is a new file, you can wait for a notification to generate new files. You can use the Win32 API in Visual Basic 6.0 to do this in Visual Basic .NET, you can use the .NET Framework class to do this. However, implementation file monitoring in .NET is more consistent with the method of completing other work in .NET, the learning curve is minimal.
You can monitor the file system using the System.IO.FileSystemWatcher .NET class. It provides some properties that allow you to set up the path of monitoring, specify that the file is also interested in changes. System.IO.FileSystemWatcher also allows you to specify file names and file types that need to be monitored (for example, *. XML refer to monitoring all XML files). Finally, you can specify the type of change in interest - for example, only the change of the new file, the file attribute changes or file size (see Listing 2). After you set up the monitoring content, you need to hook an event handler for the various events used to interested. FileSystemWatcher event has Changed, Created, Deleted, Error and Renamed. To handle an event, first you need to write an event handler that is the same as the FileSystemEventHandler agent, then add this handler to the FileSystemWatcher class. This proxy-based architecture allows you to add multiple handles to the same event, or use the same handler for multiple events - and you cannot use Visual Basic 6.0.
'System.io Contains the
'File Monitoring Classes and Types
Imports system.io
Module Module1
Sub main ()
'FileSystemWatcher Does The Real Work
DIM FW as new filesystemwatcher ()
'WaitforchangeDresult is what you
'Get Back When a Change Occurs
Dim Result As WaitforchangedResult
'set the path to monitor
fw.path = "C: / WinNT /"
'Tell it Whether to Watch Files or Directories
fw.target = watchertarget.file
'Tell it WHether to include Subdirs, INCLUDE SUBDIRS
fw.includesubdirectories = false
'Hook Up Handlers
AddHandler fw.created, _
New FileSystemEventHandler (Addressof Onfilenotify)
'enable the watcher
fw.enabled = TRUE
DO
Console.writeline ("Beginning to Monitor")
'this is where weistually wait
'Waiting Blocks Execution for the Specified Timeout
Result = fw.waitforchanged (WatcherChangeTypes.all, 60000)
Console.Writeline ("Hit Enter to Continue Q To Quit")
Loop While (console.readline <> "q")
End Sub
'This is the delegate That Gets
'Called WHEN A File Is Created
Public Sub onfilenotify (Byval Source As Object, _
Byval e as filesystemEventArgs)
Console.writeline (_
"Notification Received for File {0}, Change Type IS {1}", _e.FullPath, E.CHANGETYPE
End Sub
End module
Listing 2. Monitor your folder if there is a new file using FileSystemWatcher.
You can create a FileSystemWatcher and then set its properties. You can use AddHandler to associate with various FileSystemWatcher events, such as Created. Then you can enable FileSystemWatcher and call WaitforChanged. This call will return when a change occurs or reaches the specified timeout.
Create a Windows service
When you want to create a Windows service, you must use Visual C or use SRVANY.EXE or third-party tools in Visual Basic to create the service. Any way, you have not written your own service in Visual Basic, you just add more slave components outside the Visual Basic runtime.
If you use a C or C language, write a Windows service from the head without using the Active Template Library, you have to write a small piece of code to coordinate your service and service controller (Service Control Manager, The exchange between SCMs, SCM is used to deal with the launch, pause, continue and stopped. .NET Framework makes it easy to implement a service by providing the System.ServiceProcess.serviceBase class. To write a service, you can inherit from this class, overload some of its methods, set its properties, and then tell me!
Some methods you may have to overload include: OnStart, onstop, onpause, and oncontinue. Usually you need to load the starting working thread (Worker Thread) Once onStart, where you want to cancel the location of this working thread overstop. After you have created your own service, you can use SCM installation and register this service. This is another area, the base class in .NET Framework provides most of the required work. You can create a class inherited from System.Configuration.install.installer, exempt from System.ServiceProcess.ServiceInstaller, then set its properties, such as service startup mode and service name. This makes .NET installation utility installutil.exe installs (and deleted) your service, and you don't need to write additional code.
When you start using Visual Studio .NET, you will find that Visual Studio .NET makes it easier to use .NET Framework. Visual Studio .NET allows you to create a Windows service (Installer), monitor file system changes, write event records - all are done using designers and components rather than writing code. For example, to add a setup to your own service item, you only need to select the service designer avoidance, then click the Add Installer at the bottom of the Properties window (see Figure 3). This will add a new module ProjectInstaller.vb, which includes all code required to install the service. You can use the designer (see Figure 4), customize the installation option, such as startup mode
Figure 3. Add the installer to your service items using Visual Studio .NET
Figure 4. Using the designer, the installation option, such as STATTYPE, custom conclusions
.NET Framework allows you to use Visual Basic .NET, it is easy to develop robust applications. It allows you to focus on learning a compatible programming model and allow you to directly access the underlying .NET platform. With Visual Basic .NET and .NET Framework, you can make your own app to achieve an unprecedented level.