Multithreading – synchronize () suspends the thread

I wrote a DLL library in Delphi, which created multiple threads Let me describe the problem step by step I'm sorry to have made a long description in advance: - (

Let's forget about the library for a while I created a Windows application that will render views from several cameras I created a window to display views from a single camera, which contains a Timage control A thread (a descendant of TThread) downloads the current image from the camera every few milliseconds and assigns it to the Timage control of the window (using the synchronize () method) The application creates multiple instances of the window at startup (each window has a separate thread), so you can view real-time views from multiple cameras at the same time More importantly, all these viewing windows are the parent of the main application window, so they appear in it

Everything was fine until I decided to put these two windows into the DLL library I found it necessary for some reason, but now they don't matter So I created a new DLL library, added the existing main window and camera view window to the project, and exported a function to create and return the main window instance When the main window is created, it creates several camera view windows, making itself the parent window

Then, for testing purposes, I created an application that imports the DLL function mentioned above from the library and calls it at startup to obtain an instance of the main window; Then just display it on the screen (in an modeless state)

When I started the application, I found that I couldn't get a single image from any camera When I debug it, I notice that when a thread calls the synchronize () method, it hangs forever It did not happen before these two windows were put into DLL

This is my problem To be honest, this is my first approach to the library. So far, I have to solve many other problems You may wonder why I use windows instead of framework... So whenever I create a TFrame instance in DLL, I get an exception saying "control XXX has no parent window" I didn't know what to do, so I used windows instead

Can you tell me how to deal with the synchronization problem? When the application starts, the main thread does not seem to be blocked in any way. It accepts clicking buttons, etc So what's the problem?

Please help!

Thank you first!!

Solution

When you call TThread When you synchronize, thread and method pointers are added to classes Global synclist in PAS: TList In the tapplication.exe of the main exe The Idle routine calls CheckSynchronize, which checks SyncList, but it checks the version in exe instead of the version in DLL. As a result, your synchronization method will never be called

The simplest solution is to switch from DLL to package, which will eliminate duplicate synclist

Another way is to overwrite the application. Exe OnIdle callback and manually call checksynchronize. DLL You need some application help because your DLL also has an application object, and that doesn't work

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>