Dry at a specified time, must be dry (event dispatch in KBMMW)

Source: Internet
Author: User

Since last year, KBMMW gradually increased the connotation, in addition to improve the various services, and gradually increased and expanded as a middleware must have a function,

For example, rights Management, log system, scheduling system, memory debugging and other functions.

Today to introduce you to the KBMMW scheduling events, the main goal of the dispatch event is "to dry at a specified time, must be dry", not "Love dry", is "must Do":).

In the traditional Delphi event control room through two ways, a way through the ttimer to achieve, (I have 20 years of my alma mater cafeteria dining consumption system is through TTimer to poll

POS machine). The other is by extending the TThread thread class to complete some specified events through the background.

The use of TTimer to complete event scheduling is actually a loose dispatch, which relies on the Windows message mechanism, if an event is not done in the specified event,

Then the message will be ignored, this will lose some of the events, in many cases is not allowed.

Using the extended thread to implement scheduling, can be more accurate execution of scheduling events, but because of the complexity of multi-threaded programming, and accurate time calculation must be certain

Skill and skills are often discouraged by the general developer.

So what do we do? KBMMW to help, in the new version of KBMMW, added the Tkbmmwschedule class, this class is used to complete the event scheduling, complete

"Do it at a specified time, must do" this task. This class is easy to use, not only easy to use, but also includes a lot of related operations to start and close.

The Tkbmmwscheduler class is not only a container for any number of scheduling events, but also a manager for scheduling events. Ensure that these events are executed in the correct event.

KBMMW comes with a standard Tkbmmwscheduler class, the name is called Kbmmwscheduler, this is KBMMW for personal use, you can also used it, of course

, you can also self-willed to build a self-independent.

Ok. Let's take a quick look at an example.

Kbmmwscheduler.schedule (onscheduledevent). Everysecond (10). Activate (TRUE);

The code above is to let the scheduler do one thing every 10 seconds. We can write the code in the dispatch event.

function tform1.onscheduledevent (const ascheduledevent:ikbmmwscheduledevent): Boolean;

Begin

Call (' inaction, work ');

Result:=true;

End

So that the event has been carried out, inaction estimates will go insane:)

In order not to let inaction go insane, we need to be able to stop this event. So in order to be able to stop this event, we need to give this dispatch definition first to

It is convenient for us to stop this dispatch operation.

The following code controls the dispatch.

Var

Myevent:ikbmmwscheduledevent;

Myevent:=kbmmwscheduler.schedule (onscheduledevent). Everysecond (10). Activate (TRUE);

Well, we know what the event is, and it's good to do it back.

MyEvent. Active:=false;

In this way, do not stop the inaction (what is the ghost? )。

Of course, we can also be forced to name a scheduler, so that the latter convenient operation.

Kbmmwscheduler.schedule (onscheduledevent). Namedas (' xalion '). Everysecond (10). Activate (TRUE);

This way, you can later find the schedule by name and then manipulate the schedule

Xalionev:=kbmmwscheduler.getbyname (' xalion ');

There are two kinds of time scheduling in KBMMW, one is loose scheduling and the other is precise dispatch.

Loose scheduling is done through a thread pool, and all scheduling times share a thread pool, which guarantees the operation of events, but does not guarantee accuracy. Because it is a shared thread pool,

Therefore, the loose dispatching scheduling saves system resources, which is suitable for data backup and other occasions, such as ensuring that data is backed up around 12 o'clock every night, but there is no need at 12:00:00

In that instant, the default schedule for this event, KBMMW, is loosely dispatched. But on some occasions, we have to perform an event at exactly the specified time, such as on and off the bell, and none of us want to

It's time for class and I haven't heard the bell. This requires accurate event scheduling, in order to accommodate this requirement, KBMMW creates a separate thread for it to ensure

This event is executed precisely on time, at the expense of increasing the use of system resources (with the exception of course, if the system itself is busy, such as CPU load 100%, the immortal can not).

Establishing accurate event scheduling in KBMMW is also very simple, the code is as follows,

Kbmmwscheduler.schedule (onscheduledevent). Namedas (' xalion '). Everysecond (10). Precise.activate (TRUE);

Of course, you can also define loose scheduling with the following code.

Kbmmwscheduler.schedule (onscheduledevent). Namedas (' xalion '). EverYsecond (10). Relaxed.activate (TRUE);

KBMMW not only supports the second execution interval, but also many interval methods.

Everymsecond (n)---every n milliseconds, what's so fast?

Everyminute (n)-------every n minutes to measure the heartbeat?

Everyhour (n)---------every n hours, when the n=1, 8 times can get off work? No way

Everyday (n)-----------every n days, when N=1, is the clock of the program ape

Everymonth (n)----------every n months, when n=1, what is it?

Everyyear (n)--------------you develop a system that can N-year do not restart?

In fact, this n can be a decimal, everyday (0.5) is half a day.

Attention, a very humane point of appearance

Everyday (1). Everyhour (6) How much is it? Hehe, actually is everyhour (30), 24+6=30 hour.

So Everymonth (2.5). Everyhour (3) How much is it? How long is the 2.5-month period? Debug it yourself.

What do I have to do every day of the week?

Atweekdays ([Mwwdsunday,mwwdwednesday])

Execution within a specified date range is also simple

. Startingat (tkbmmwdatetime.create (' 2015-11-22t15:00:00gmt ')).

. Endingat (tkbmmwdatetime.create (' 2015-12-22t15:00:00gmt ')). If you want to wait a moment before execution, it's no Problem delayinitial (Tkbmmwduration.create (10)). You can also just let this dispatch event do only one time.   Occurs (mwsoonce). Of course, you can also replace the dispatch event Fscheduler.schedule (const ascheduledevent:ikbmmwscheduledevent) by writing an anonymous function: BOOLEANBEGINMDATA.LINES.ADD (' anonymous: ' +ascheduledevent.name); Result:=true; END). Namedas (' Relaxed event every 2 secs '). Everysecond (2). Synchronized.activate (TRUE); Do not know kbmmw such a dispatch, whether can meet your needs? If not, then you can write it yourself, I will not write it anyway.

Dry at a specified time, must be dry (event dispatch in KBMMW)

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.