How to resolve the algorithm Events step by step in the Delphi programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Events step by step in the Delphi programming language

Table of Contents

Problem Statement

Event is a synchronization object. An event has two states signaled and reset. A task may await for the event to enter the desired state, usually the signaled state. It is released once the state is entered. Releasing waiting tasks is called event notification. Programmatically controlled events can be set by a task into one of its states. In concurrent programming event also refers to a notification that some state has been reached through an asynchronous activity. The source of the event can be: Event is a low-level synchronization mechanism. It neither identify the state that caused it signaled, nor the source of, nor who is the subject of notification. Events augmented by data and/or publisher-subscriber schemes are often referred as messages, signals etc. In the context of general programming event-driven architecture refers to a design that deploy events in order to synchronize tasks with the asynchronous activities they must be aware of. The opposite approach is polling sometimes called busy waiting, when the synchronization is achieved by an explicit periodic querying the state of the activity. As the name suggests busy waiting consumes system resources even when the external activity does not change its state. Event-driven architectures are widely used in GUI design and SCADA systems. They are flexible and have relatively short response times. At the same time event-driven architectures suffer to the problems related to their unpredictability. They face race condition, deadlocking, live locks and priority inversion. For this reason real-time systems tend to polling schemes, trading performance for predictability in the worst case scenario.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Events step by step in the Delphi programming language

Source code in the delphi programming language

program Events;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, Windows;

type
  TWaitThread = class(TThread)
  private
    FEvent: THandle;
  public
    procedure Sync;
    procedure Execute; override;
    constructor Create(const aEvent: THandle); reintroduce;
  end;

{ TWaitThread }

constructor TWaitThread.Create(const aEvent: THandle);
begin
  inherited Create(False);
  FEvent := aEvent;
end;

procedure TWaitThread.Execute;
var
  res: Cardinal;
begin
  res := WaitForSingleObject(FEvent, INFINITE);
  if res = 0 then
    Synchronize(Sync);
end;

procedure TWaitThread.Sync;
begin
  Writeln(DateTimeToStr(Now));
end;

var
  event: THandle;

begin
  Writeln(DateTimeToStr(Now));
  event := CreateEvent(nil, False, False, 'Event');
  with TWaitThread.Create(event) do
  try
    Sleep(1000);
    SetEvent(event)
  finally
    Free;
  end;
  Readln;
end.


  

You may also check:How to resolve the algorithm Literals/String step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Count in factors step by step in the D programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the i programming language
You may also check:How to resolve the algorithm Binary digits step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Jacobi symbol step by step in the Julia programming language