Several methods of adding timing mechanisms in VC

zhaozj2021-02-16  46

The timing mechanism refers to the specified event when the program is running a specified event. When programming under DOS, it is mainly implemented by the clock interrupt INT 8 and its call interrupt INT 1CH. The application is triggered by modifying these system interrupts. Under Windows, if you imagine that there is no tricks in DOS, it is unrealistic, then how should I achieve timing mechanism? Here are some of the experiences in the study of this problem, and several programs are proposed for your reference.

The first solution is a way that everyone is familiar with intercept timing messages. In Windows, there is a system resource called "Timer", which is called "Timer", and a message that is WM_Timer is received every time a program for this type of resource is applied. The code that needs to be performed timed can be placed in the processing section of the message. If in VC, we can implement this in the following steps:

Use MFC AppWizard to create a standard project to accept all default options. The "CMAINFRAME" class named S1 is selected in ClassView, then press CTRL W to activate ClassWizard, select "CMAINFRAME" in the "Message Map" tab, followed by "WM_TIMER" in "Message", finally pressed "Add ". The above steps have been added to the mapping process of the WM_TIMER message. Back to ClassView, double-click the "oncreate" member function, add the statement of the application Timer at the end of the function: setTimer (100, 1000, null); // Apply a Timer with a marking value of 100, fixed time separation to 1000 milliseconds (1 second ). Double-click the ONTIMER function in "ClassView" to enter the code that you want to implement. In this example: MessageBeep (1000) ;; // Send and execute every other second, we can hear the sound every other second. This is exactly what we need to do in the ONTIMER function.

In practice, we can change "MessageBeep (1000);" to change the task we want to complete, such as timing stores, etc.

The second solution also uses Timer resources, but it is a good code & # 0; & # 0; we can join a component with timed functionality to the current project. This method is especially suitable for dialog-based projects. Specific steps are as follows:

Create a dialog-based project with MFC AppWizard, and accept all default options. Named S2. In ResourceView, double-click IDD_S2_DIALOG, display the dialog, and change it "to do:" to "Example of Time Trigger Demo", indicating the role of the project. Right-click on the dialog box editing area, select "Insert ActiveX Control" in the pop-up, select "Timer Object" from the pop-up list box, and a Timer object will appear after the dialog box will appear. Let's right-click Timer object, select "Properties" from the pop-up menu, then select the "All" tab, set the interval value to 5000, which occurs once every 5 seconds. Timer event. Back to the dialog editing interface, double-click Timer, generate a CS2DLG :: ONTIMERTIMER1 member function, accept the default value, and enter some input: MessageBox ("Timed Trigger Message Box", "Timed Demo", MB_OK; Compile and Running this project will pop up a message box every 5 seconds during the generated dialog.

Also, we can replace the message box statement in 5 in any own code. See example in detail S2. The third method is to adopt thread technology. As is well known, Windows 9X is a multi-threaded multi-task operating system, which is scheduled by the system division in the core as the basic unit of the scheduling in the kernel. With this, we can create a "duty" timing thread in the program, trigger the code of the task we have to complete through the synchronization between the thread. Not like the first two methods requires at least one window as the main window of accepting messages, using thread technology implementation timing triggers will exempt the trouble of creating windows, and consumption of various resources brought. Let's take an example to illustrate this problem: We don't create a main window in the CMYAPP class, but create a working thread, and then sleep the Something function of the main thread after a certain time. In order to support the running of threads, we need to add corresponding thread functions to the CMYAPP class. Below, we are still a step-by-step implementation:

Create a standard project with MFC AppWizard, where do not produce unnecessary code, no document / view support, and select a single document. The project is named S3. Use "/ * ... * /" in cs3app :: InitInstance () to comment all the code before "Return True;". This is not to establish a window. And add the following code: EXITFLAG = true; // whether the logo variable of the loop of the main thread is ended. Since the sub-thread relies on the main thread, in this example, in order to avoid no main window, end the application in advance, so that the sub-thread cannot exist, so give the main thread a loop, knowing that the global variable EXITFLAG is set before the sub-thread exits False .startThread (); // Start thread do {} while (exitflag); // until the end of the sub-thread :: MessageBox (NULL, "Main thread ends!", "Timed Demo", MB_OK; Return True; Add a marker variable "exitflag" in Globals, type BOOL. It is used by the main thread to determine if it ends its own operation. By ClassView's public part declares the following functions: void startthread (void); // Start the thread static uint threadfunction (void); // Main execution code STATIC uint staticThreadFunc (LPVOID LPPARAM); // Set the thread The function needs to be particularly pointed out that when the thread setting is made with AFXBEGINTHREAD, the first parameter must be stated as static in this example, otherwise the error of the parameter conversion will not be peaceful. Enter the following code in StartThread: AFXBEGINTHREAD (staticthreadfunc, this); // Establish and start the thread Enter the following code in StaticThreadFunc: Return ThreadFunc (); // Call the function of completing the main thread code, pay attention to static. Implement ThreadFunction: INT i; i = 5; // Trigger 5 times while (i -) {Sleep (5000); // Interval 5 seconds: MessageBox (null, "I was triggered!", "Timed Timed Demo", MB_OK);} EXITFLAG = false; // EXITFLAG is a global variable that informs the main thread to run. RETURN 0;} Compile and run the project, you will not see the application window, but you can see a message box every 5 seconds, the desktop appears, and the message box ends in the main thread is pop up after 5.

The above is some of the methods taken by this person to achieve timed triggers under WINDOWS, and the features of their own methods are also pointed out in the introduction. I hope that I can help you, and I hope you can get everyone's correct. If you have any comments and ideas, welcome E-mail to me (Yangshanhe@21cn.com). ==

Earlier 2000 in 2000, I gathered together, so I didn't know anything.

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

New Post(0)