Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3330592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:30:23+00:00 2026-05-17T23:30:23+00:00

In Delphi 2009 and Windows API, is there a way to detect that a

  • 0

In Delphi 2009 and Windows API, is there a way to detect that a particular piece of code is running in the context of a secondary thread? In pseudocode, I would like to be able to say:

procedure DoSomething;
begin
  if InvokedBySecondaryThread then
    DoIt_ThreadSafeWay
  else
    DoIt_RegularWay;
end;

This is for a logging library which I wrote and have used for years, and am now trying to adapt to a situation where one procedure can be invoked from multiple threads. My “regular way” is not threadsafe. I know how to make it threadsafe, but I’d like to use the threadsafe method only when actually necessary.

Explanation (not a required reading 🙂

It boils down to a choice between using SendMessage and PostMessage to dispatch a logged message to multiple receivers, such as a logfile, console or a VCL control. Using PostMessage means that the messages will not be received while a long blocking operation is in progress, which defeats the purpose of logging somewhat, esp. when used to indicate progress. I suppose I could protect the SendMessage call with a critical section, but again I would prefer to have to do it only when truly required.

I know about the global var IsMultiThread in system.pas, but this will only tell me that the application has started secondary threads. These may be threads created by 3rd party libraries, and as such they will never access “my” code, so their existence will not affect my logging logic.

I really wish I could use the same low-level library code regardless of whether it is invoked from one or multiple threads. It would be easy for example to call modified, threadsafe logging procedures from inside secondary threads, but this would duplicate much code, and I’d still have to remember to always do the right thing.

@Lieven: Currently, the logging logic goes like this, somewhat simplified

I want the logging to be as painless as possible, with minimal setup code and no worrying about managing object lifetime, so the library only exposes a number of overloaded helper procedures, such as

procedure Log( const msgText : string; level : TLogLevel = lvNotice ); overload;
procedure Log( const msgText : string; Args : array of const; level : TLogLevel = lvNotice ); overload;
etc, including specialized routines that log a StringList, a boolean, an Exception and so on

Pretty much everything else happens in the implementation of the unit. All the helper routines eventually end up calling

procedure _LogPostMessage( const msgText : string; level : TLogLevel );

which (a) checks if the singleton dispatcher object has been initialized; (b) creates an instance of a TLogMessagePacket object (container for the message text, timestamp etc.), and finally (c) does a SendMessage or PostMessage to send the “packet” to the dispatcher (which has a window handle to receive it).

Then there is a group of classes descended from an abstract TLogReceiver class. One such class receives the logged messages and writes them to file, another updates a TMemo, etc. I instantiate the concrete receivers I want to use in a project, and register them with the dispatcher, which owns them from that point on.

When the dispatcher receives a message “packet”, it hands it off to each receiver in turn, then frees it.

So I am probably fixed in the above way of thinking, which is why I am not quite getting your idea when you say code handing a dispatcher object to the logging library should choose one depending on thread context. The dispatcher is really the main engine of the library, and only one exists at a time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:30:23+00:00Added an answer on May 17, 2026 at 11:30 pm
      if GetCurrentThreadID = MainThreadID then begin
    // in main thread
      end
      else begin
    // in secondary thread
      end; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Delphi 2009 I'm finding that any time I use TThread.CurrentThread in an application,
I just got Delphi 2009 and have previously read some articles about modifications that
One of Delphi 2009's advertised features was PNG support. That's great, because the Unicode
I`m using Delphi 2009 and want to operate some XML data. I heard that
How to dim / fade all other windows of an application in Delphi 2009.
Why will this not compile in Delphi 2009? unit VistaFolders; interface uses Windows, ShellAPI,
Delphi 2009 has changed its string type to use 2 bytes to represent a
Delphi 2009, among some cool stuff, has also just got Anonymous methods. I've seen
Delphi 2009 introduced a hierarchical system for project options configuration, where you set base
In Delphi 2009 whereabouts do you turn on the option to treat warnings as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.