Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute not TThread based thread in main thread?

Tags:

delphi

For example from thread provided by callback from CreateTimerQueueTimer in executable or dll? It is significant to have same thread id as main thread.

procedure TMyMainClass.ExecuteMe(someparam: paramtype);
begin
  {something}
end;

and

procedure TimerCallback(pvContext: pointer; fTimerOrWaitFired: boolean); stdcall;
begin
  {do what ?}
end;

Final update:
All this stuff (TThread.Synchronize, TThread.Queue, PostThreadMessage etc) works through messages. So be sure host application of your dll processing messages while waiting for callback.

like image 309
user2091150 Avatar asked Apr 06 '13 21:04

user2091150


People also ask

Why should you avoid to run non UI code on the main thread?

If you put long running work on the UI thread, you can get ANR errors. If you have multiple threads and put long running work on the non-UI threads, those non-UI threads can't inform the user of what is happening.

How Stop main thread while another thread is running?

You can not stop the main thread while any other thread are running. (All the child threads born out of main thread.) You can use function Thread. join() to keep the main thread waiting while other thread(s) execute.

Which method is used for main thread?

A thread can be created by implementing the Runnable interface and overriding the run() method. The Main thread in Java is the one that begins executing when the program starts.

Can we decide order of execution of threads?

You cannot tell the thread scheduler which order to execute threads in. If you need to ensure that a certain piece of code which is running on thread A must run before another piece of code running on thread B, you must enforce that order using locks or wait() / notify() .


1 Answers

To execute code in the main thread, without access to a TThread instance, call the class methods TThread.Synchronize or TThread.Queue.

If you happen to be using an old Delphi compiler that does not have those methods, then SendMessage or PostMessage with a user defined message are the simplest solution.

like image 177
David Heffernan Avatar answered Nov 06 '22 13:11

David Heffernan