Source Code TCriticalSection Threrad Example Delphi

emailx45

Social Engineer
Joined
May 5, 2008
Messages
2,387
Reaction score
2,149
TCriticalSection Threrad Example Delphi
Ivan Revelli - 15/May/2020
[SHOWTOGROUPS=4,20]
Example on how to user a critical section in Delphi:

Delphi/Pascal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
TThreadedMsgEvent = class( TThread )
private
FLock : TCriticalSection;
FStr : TQueue<String>;
FMemo : TMemo;
function GetEvent : String;
protected
procedure Execute; override;
public
procedure AddEvent( aMsg : String );

constructor Create( AMemo: TMemo );
destructor Destroy; override;
end;
implementation

{ TThreadedMsgEvent }

procedure TThreadedMsgEvent.AddEvent(aMsg: String);
begin
FLock.Acquire;
FStr.Enqueue( FormatDateTime('DD/MM/YY HH:NN:SS.ZZZ',Now)+ ' : '+ aMsg );
FLock.Release;
end;

constructor TThreadedMsgEvent.Create(aMemo: TMemo);
begin
inherited Create(True);

FreeOnTerminate := False;
FOnMessage := ACallBack;
FStr := TQueue<String>.Create();
FLock := TCriticalSection.Create;
FMemo := aMemo;
Resume;
end;

destructor TThreadedMsgEvent.Destroy; override;
begin
FreeAndNil( FStr );
FreeAndNil( FLock );
end;

procedure TThreadedMsgEvent.Execute;
begin
while not Terminated do
begin

try
if (FStr.Count > 0) then
begin
if Assigned( aMemo ) then
begin
TThread.synchronize( procedure
begin
FMemo.Lines.Add( GetEvent );
end; );
end;

end;
except
end;
TThread.Sleep(1);
end;

end;

function TThreadedMsgEvent.GetEvent: String;
begin
FLock.Acquire;
result := FStr.Dequeue;
FLock.Release;
end;

From post found at: https://stackoverflow.com/questions/17506615/how-to-handle-log-in-a-threaded-manner-in-delphi

In short:
How to handle Log in a Threaded manner in Delphi
Active 6 years, 10 months ago - Viewed 3k times

Delphi XE2
I have a form with a TMemo that I want to show what is going on in several services started by the application.
What I have running:
  • idHTTPServer running with idContext responding to requests
  • a Thread downloading updates from Dropbox
  • idUDPServer responding to UDP requests
  • another thread taking care of some database stuff.
  • the main application thread also needed to add log
Basically, I need to know how to create a standard, unified, thread safe way to channel the log messages to my TMemo and keep the user updated of what is going on.
....


[/SHOWTOGROUPS]
 

Eddie1331

Member
Joined
Jan 25, 2017
Messages
5
Reaction score
0
"Hey, I just threw together a simple example of a critical section in Delpi. Not sure if it's the most elegant solution, but it should give you an idea of how it works: https://pastebin.com/9rK4XvYd Let me know if you have any questions about it."
 

andrjuha777

Member
Joined
Feb 14, 2011
Messages
6
Reaction score
0
"Hey OP, I just tried the example you posted and it works great for small threads. However, I did experience some performance issues with a high number of threads competing for the lock. Does anyone else have any experience optimizing TCriticalSection performance?"
 

weektor

Member
Joined
May 20, 2008
Messages
5
Reaction score
0
"Hey OP, I've got some experience with threading in Delphi. Check out the Indy components, they have some great stuff for synchronizing threads. Specifically, the TCriticalSection component can help you achieve the functionality you're looking for."
 

Verg

Member
Joined
Oct 14, 2009
Messages
8
Reaction score
0
Not an expert on Delphi, but I've used similar threading libraries on other platforms. Can you tell me more about what you're trying to achieve with a critical section in your thread implementation? Maybe we can brainstorm a solution or find an example online.
 
Top