How to improve performance with multithreading?

I have a program that receives string messages from other applications and parses them using VCL. Messages are sent as follows:

AtomId := GlobalAddAtom(PChar(s)); SendMessage(MyProgramHandle, WM_MSG, 0, AtomID); GlobalDeleteAtom(AtomID); 

My program receives this message, analyzes it for a while, and then returns control to the application. It takes time to analyze one message to degrade the performance of other applications.

One possible solution is to create a form with the same title and the same class in another thread and rename the class of the main form. But as far as I know, it is not recommended to create forms in streams.

So what are the possible ways to improve performance?

+4
source share
2 answers

So the problem is that both receiving messages and VCL operations are performed in one thread (main VCL thread)? And therefore, reception and processing are serialized, and as a result, senders are blocked when your application is busy filling the grid? Then I can understand that you are asking for a way to move the reception to another windowed message loop.

Therefore, I would create a window (and not a VCL form) just to receive messages and use its message loop to add a message to the queue. Therefore, you only need to find this (non-VCL) window and SendMessage for its handle. In a VCL stream, a timer can receive the following "n" messages and add them to the grid.

+2
source

A typical approach would be to create a workflow (or pool of workflows). The main thread will continue to receive messages, but instead of parsing them, it will simply add them to the queue (for example, a linked list).

The worker thread takes the first item in the queue and processes it. Upon completion, it returns to the queue to receive the next item.

Since a queue is a shared resource between multiple threads, you need to control access to it. The mutex ensures that only one thread gets access to the queue at any given time.

Good luck.

+6
source

Source: https://habr.com/ru/post/1393901/


All Articles