Saturday, 10 December 2016

Simple background tasks with OtlParallel - Life after 2.1: Async redux

OtlParallel unit defines four overloaded Async methods Parallel.Async( class procedure Async(task: TProc; taskConfig: IOmniTaskConfig = nil); overload; class procedure Async(task: TOmniTaskDelegate; taskConfig: IOmniTaskConfig = nil); overload; class procedure Async(task: TProc; onTermination: TProc; taskConfig: IOmniTaskConfig = nil); overload; class procedure Async(task: TOmniTaskDelegate;...

Thursday, 8 December 2016

FastMM – Preparing your apps to report memory leaks

One of the most challenging parts of inheriting a legacy project is to fix the memory leaks that most often are hiding in the code. A while ago, while dealing with an application that managed to eat all the available memory within a few hours I found FastMM. And it sure was a great find. What is FastMM? FastMM is a memory manager replacement designed to be used with Delphi and C++ Builder. It is...

Monday, 5 December 2016

1.6 Locking vs. Messaging

I believe that locking is evil. It leads to slow code and deadlocks and is one of the main reasons for almost-working multithreaded code (especially when you use shared data and forget to lock it up). Because of that, OmniThreadLibrary tries to move as much away from the shared data approach as possible. Cooperation between threads is rather achieved with messaging.If we compare shared data approach...

1.5 Tasks vs. Threads - OmniThreadLibrary

1.5 Tasks vs. Threads In OmniThreadLibrary you don’t create threads but tasks. A task can be executed in a new thread or in an existing thread, taken from the thread pool. A task is created using CreateTask function, which takes as a parameter a global procedure, a method,...

Introduction to OmniThreadLibrary [2]

1.3 Installation Download the last stable edition (download link is available at the OmniThreadLibrary site, or download the latest state from the repository. Typically, it is safe to follow the repository trunk as only tested code is committed. [Saying that, I have to admit that from...

Introduction to OmniThreadLibrary

OmniThreadLibrary is a multithreading library for Delphi, written mostly by the author of this book (see Credits for full list of contributors). OmniThreadLibrary can be roughly divided into three parts. Firstly, there are building blocks that can be used either with the OmniThreadLibrary threading helpers or with any other threading approach (f.i. with Delphi’s TThread or with AsyncCalls)....

3.4 Task Controller Needs an Owner

Task Controller Needs an Owner The IOmniTaskController interface returned from the CreateTask must always be stored in a variable/field with a scope that exceeds the lifetime of the background task. In other words, don’t store a long-term background task interface in a local variable. The simplest example of the wrong approach can be written in one line: CreateTask(MyWorker).Run; This code looks...

Low-level Multithreading with OmniThreadLibrary

The low-level OmniThreadLibrary layer focuses on the task concept. In most aspects this is similar to the Delphi’s TThread approach except that OmniThreadLibrary focuses on the code (a.k.a. task) and interaction with the code while the Delphi focuses on the operating system primitive required for executing additional threads (TThread). A task is created using the CreateTask function, which takes...

Sunday, 4 December 2016

Delphi Program Name,Process ID,Window Handle

// Get ProcessID By ProgramName (Include Path or Not Include) function GetPIDByProgramName(const APName: string): THandle; // Get Window Handle By ProgramName (Include Path or Not Include) function GetHWndByProgramName(const APName: string): THandle; // Get Window Handle By ProcessID function GetHWndByPID(const hPID: THandle): THandle; // Get ProcessID By Window Handle function GetPIDByHWnd(const...

Getting the parent process filename of a PID, using Delphi

uses Psapi, Windows, tlhelp32, SysUtils; function GetParentProcessFileName(PID : DWORD): String; var HandleSnapShot : THandle; EntryParentProc : TProcessEntry32; HandleParentProc : THandle; ParentPID : DWORD; ParentProcessFound : Boolean; ParentProcPath : PChar; begin ParentProcessFound := False; HandleSnapShot ...

Friday, 2 December 2016

Wait for thread without freezing the application - Indy [example]

Use the TThread.OnTerminate event to know when the thread has finished: type TSendThread = class(TThread) private http : TIdHTTP; Line: string; procedure AddLine; protected procedure Execute; override; public constructor Create; reintroduce; destructor Destroy; override; URL : String; Method : String; property ReturnValue; end; constructor TSendThread.Create; begin ...

Thursday, 1 December 2016

Create a Job object And terminate Child Process

How to gracefully handle when main.exe terminated, child.exe terminated also? You need to use jobs. Main executable should create a job object, then you'll need to set JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE flag to your job object. uses JobsApi; //... var jLimit: TJobObjectExtendedLimitInformation; hJob := CreateJobObject(nil, PChar('JobName'); if hJob <> 0 then begin jLimit.BasicLimitInformation.LimitFlags...

Run an application and get the handle to its window

Sometimes, when we start an application programmatically, it is useful to have a handle to it's window. The process of how to do this is described below. 1. create a new unit utils.pas: unit utils; interface uses Controls, Graphics, StdCtrls, ExtCtrls, ComCtrls, Buttons, Dialogs, Classes, SysUtils, Windows, ShellApi, Forms; procedure CloseMessage (process_id : Cardinal); // get proc id function...