?
Solved

[threads] APC?

Posted on 2003-03-25
5
Medium Priority
?
832 Views
Last Modified: 2008-08-10
according to MDSN.
Asynchronous Procedure Calls
An asynchronous procedure call (APC) is a function that executes asynchronously in the context of a particular thread. When an APC is queued to a thread, the system issues a software interrupt. The next time the thread is scheduled, it will run the APC function. APCs made by the system are called "kernel-mode APCs." APCs made by an application are called "user-mode APCs." A thread must be in an alertable state to run a user-mode APC.

When are you making an APC? when a public method of a Tthread-descendant is called from the main thread? (for instance)

Thus if the thread is in a waitstate via SleepEx(1,true), the thread can wake-up when a thread-function is called from the main-thread?
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Author Comment

by:TicketToTotalitarianism
ID: 8207026
I guess that when I call a public Thread-function from the main thread, it's executed in the timeslice of the thread? or the other way around? (not apc?)
0
 
LVL 20

Accepted Solution

by:
Madshi earned 800 total points
ID: 8260559
I'm not sure exactly what you want to know.

Delphi doesn't use APCs, as far as I know. If you call a TThread method from the main thread, it's executed in the main thread.

Does that answer your question?
0
 

Expert Comment

by:CleanupPing
ID: 9316607
TicketToTotalitarianism:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 

Author Comment

by:TicketToTotalitarianism
ID: 9449811
that's too bad ... I needed an APC, to let the thread sleep, and wake-up when needed..

SleepEx

The SleepEx function suspends the current thread until one of the following occurs:
- An I/O completion callback function is called
- An asynchronous procedure call (APC) is queued to the thread.
- The minimum time-out interval elapses
0
 

Expert Comment

by:bartland
ID: 11166884
The correct way to do this is to use a signalled event. In Delphi this is a TEvent object. For each Thread class you create put a TEvent object is a member variable. This way every thread can be wakeable.

  TEThread = class(TThread)
  private
    m_Event: TEvent;
    m_Paused: boolean;
  public
    procedure Sleep(Timeout: DWORD);
    procedure WakeUp;
    procedure Pause(pause: boolean);
  protected
    procedure Execute; override;
  published
    property Paused: boolean read m_Paused;
  end;

procedure TEThread.Sleep(Timeout: DWORD);
begin
  m_Event.ResetEvent;
  m_Event.WaitFor(Timeout);
end;

procedure TEThread.WakeUp;
begin
  m_Event.SetEvent;
end;

// BTW. If you want to pause the thread, be wary of using suspend/resume. These functions will stop your thread processing completely whereas you may just want to allow processing to complete but not do any more until unpaused. suspend/resume calls must also be balanced which is tricky if calling them from different threads (inside and out).

procedure TEThread.Pause(pause: boolean);
begin
  if m_Paused <> pause then begin
    m_Paused := pause;
    if not m_Paused then WakeUp; // may as well wake it up.
  end;
end;

procedure TEThread.Execute;
begin
  try
    while not Terminated do begin
      Sleep(..sometime..);
      if not Terminated and not Paused then begin
        .. do stuff ..
      end;
   end;
  except
    // don't allow unhandled exceptions or the thread will GPF - bad if running from, say, a service.
  end;
end;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question