The fact that a Twain object requires a window handle in its object constructor suggests that something inside the Twain object requires message processing. Handling end-to-end messages is complicated, but even more so when it happens inside the API.
If the twain API creates a window handle (open, for example, a pop-up window or dialog, or secretly, for example, for interprocess communication (IPC)) as part of one of the API functions that you call from the background thread, this window handle will be associated with the thread that was created on - background thread. All messages sent to this window handle will be queued, waiting for the background thread to process them in the message loop. You do not have a message loop in the background thread, so the window handle will be stuck in limbo. It will not respond to window messages. Sent messages will remain unanswered. SendMessage () will be blocked.
Even if this is not a window descriptor / message problem, it is very likely that if the Twain API was not explicitly and deliberately implemented taking into account multithreading, it will have problems when used in streams. You create a twain object in one thread and then use it in another thread, so this is a cross-thread situation. If you could create a twain object in a background thread and use only a twain object in the context of this background thread, this could bypass the thread proximity problems in the twain API implementation. When it comes to descriptors and messages, moving everything into the background thread also makes the situation worse.
The ability to use the facility in streams is not provided for free. If the twain API was not intended to be used in streams, you can do this to make it work in streams. It is best to keep the Twain object in the main user interface thread.
source share