Random event in VB program

zhaozj2021-02-08  196

In the programming process, how to easily handle numerous random events, often one of the problems that the large system must first consider. When developing a Windows program with a C language, you can easily use the message mechanism (Message), but when you design the VB program, there is no such convenient condition. For example, multiple windows simultaneously open the same table (Table), modifying the data in one window, while other windows can also perform data updates, then there is a message that indicates that data changes Broadcast between all windows. This is required if the language used is C, just define a user message (userMessage). But if you use VB programming, it is not so simple to do. The VB5.0 Enterprise Edition adds a lot of strong features, there are several characteristics, just able to help us solve the problem. Let's introduce these features:

1. User-defined events: In the class module, you can use the Event keyword to define user-defined events, using the RaiseEvent statement to generate this event, this mechanism gives great convenience to handling random events. When receiving a message requested to broadcast, a predefined event is generated, and the object object of the message needs to be processed, and only the event is completed, the message is completed.

2.ActiveX EXE Part: Using VB, you can easily package the shared code in the ActiveX part. In an ActiveX component, the message broadcast engine is not only convenient for use in the program, but more importantly, you can implement messaging between cross-processes. Because the ActiveX part has two internal (DLL) external (EXE), for external components, sharing can be shared in the module (about two code components of ActiveX).

Different, please read the VB online help file).

3. Remote automation connection: ActiveX components are a standard client / server structure that uses the COM model of the Windows platform, VB has conveniently extend this structure to the range of the entire network. Therefore, our message broadcast design, after implementing messaging between processes, thereby implementing messaging on the network, also possible.

Through the above introduction, the design idea of ​​this method is more clear. In the specific design, through the mutual collaboration between the four modules, the message transmission, broadcasting and reception are completed, and these four modules Package in an ActiveXexe component. Below is a brief introduction of these three class modules and source code:

One of the class modules: msg.cls ---- In this module, the message data structure VBMSG class is defined, which is the carrier in the message transfer. Here is just a simple example. If you want to implement more features, such as the two-point data channel, not a simple broadcast message, you may need some expansion.

Option expedition

/ Said:

/ Message: Declining the message structure of the whole situation

Public ITYPE AS Long / Message Type Number

Public iname as string / message name

Public isource as string / message sources

Public IDESCRIPTION As String / Meeting

DIM IT As Date / Message time

/ Return to Japan time

Public property get itime () as dateitime = it

End Property

/ Return character time

Public property get itimester () AS String

ITIMESTR = Format (IT, "YYYY.MM.DD HH: mm: SS")

End Property

/ When the pair is established, set the message.

Private sub coplass_initialize ()

IT = now ()

End Sub

Two of the class module: msgcli.cls ---- This module is the definition of the client's receiving end MSGClient class, which is equivalent to one

Message receiver. A ReCMSG event defined in this class, when the receiver receives the message (process setmsg

Calling), generates this event. The establishment of the receiver intercepts this event and processes the message. In order to avoid receiving

The necessary news, declare the two variables of minmsg, maxmsg to pass the ITYPE attributes in VBMSG

filter.

Option expedition

'Said:

'Customer message receive class

'Defining a message, the host class of this pair should be intercepted,

And to receive the message received.

Public Event Recmsg (Byval MSG As VBMSG)

'Connecting to the settings of the message, you can filter off the news that you don't need.

Public MinMsg As Long

Public Maxmsg As Long

'The log name of the object, the value should not be modified when used

Public ID As long

'The income of the event should only be called by the Message Server (MSGServer)

Public SUB SETMSG (MSG As VBMSG)

If msg.itype> = minmsg and msg.itype <= maxmsg the raiseevent recmsg (msg)

End if End Sub ''

According to ID, return to the keyword of the object, only by the Message Server (MSGServer)

Public property get key () AS STRING Key = "ID:" & ID End Property

Three class modules: Global.bas ---- This module declares two global variables, one is a list of receiver, one is a receiver counter, and pericate each receiver allocated a unique ID flag . Place the variable in a separate module, is to achieve the sharing between the data in the process, is the key to the inter-process messaging. (Should be guaranteed that the engineering is single-threaded, otherwise data sharing cannot be implemented.).

Option expedition

/ Said:

/ Message server full-time variable

/ Message receives the guest list

Public Clients As New Collection

/ Message receive customer ID counter

Public Clicount As Long

Class module 4: msgsrv.cls --- This module defines the message server class MSGServer, which is the main body of the message broadcast engine. It mainly manages the maintenance message receiver list (Clients), which will be sent (call The SendMSG process) sends all receivers in the list. Note that this class is declared as a public overall class, which is mainly for convenience (without having to build this class in each program, the process name is globally effective). Option expedition

/ Said:

/ Metrics

/send messages

Public SUB Sendmsg (MSG AS VBMSG)

DIM C as msgclient

For Each C in Clients

C.SETMSG MSG

Doevents

Next C

End Sub

/ Increase the message to receive the customer

Public Sub AddmsgClient (c as msgclient)

Clicount = CLICOUNT 1

C.ID = CLICOUNT

Clients.Add C, C.Key

End Sub

Public Sub DelmsgClient (c as msgclient)

Clients.Remove C.Key

If Clients.count = 0 Then Clicount = 0

End Sub

Here, a small and flexible message broadcast engine is completed, it is very wide, it is also very convenient, just introduce the compiled ActiveX component in the project, you can directly call sendMSG send messages, possibly The message receiver (MsgClient) will be a bit a bit troubles, and a simple application example will now explain:

When designing a Windows program, it often feels that the actual operation of the program is very far from the difference you imagined. When debugging, you will be very hoped to see some of the procedures running the background. Single-step execution or debug commands using VB will be subject to restrictions. With the message broadcast engine, make a universal real-time message event to view the program, you can solve this problem well. The main job of the program is to capture a set of predetermined message events and display the contents of the message in the list box, which can be done with one form, which is generally as follows:

Const msginfoid = 101

Private Withevents Mclient As MsgClient

Private sub flow_load ()

Set mclient = new msgclient

Mclient.minmsg = msginfoid

Mclient.maxmsg = msginfoid

AddmsgClient Mclient

End Sub

Private Sub Form_Unload (Cancel AS Integer)

DelmsgClient Mclient

End Sub

Private Sub Mclient_Recmsg (Byval Msg As Vbmsgsrv.vbmsg)

List1.additem msg.itimestr & chr (9) & msg.iname & chr (9) & msg.IDESCRIPTION

End Sub

In the debugged program, for the convenience of call, you can write a global process, like the following:

Const msginfoid = 101

Public Sub MsgInfo (INAME As String, IDES AS String)

DIM MSG As New MsgClientWith MSG

= INAME

Idescription = IDES

End with

Sendmsg MSG

End Sub

Inserting the program's key to insert the MSGINFO process, running information is displayed in the window of the event view program. This method is especially suitable for commissioning software systems for multi-program collaboration. When the software system is officially given to the user, the inserted MSGINFO process does not have to be deleted, as long as the real-time viewing is written to the log file, these runtime information is also the first-hand information for future software maintenance.

转载请注明原文地址:https://www.9cbs.com/read-2348.html

New Post(0)