tmyroadctfig / twaindotnet

MIT License
179 stars 117 forks source link

Using twaindotnet in a thread... #3

Open mForEquality opened 11 years ago

mForEquality commented 11 years ago

I am really stuck on utilizing this codebase because in my case, interaction with the scanner must be on a separate thread from my MDI style windows form application.

My form app that starts a background thread (BGthread). The BGthread creates a System.Collection.Concurrent.BlockingCollection and processes the commands that get added to the collection. This BGthread uses the TwainDotNet classes to interact with the downstream scanner.

My MDI app puts a command into the collection monitored by the BGthread. For example, an instance of command would be a command to instantiate the TwainDotNet.Twain class. A follow-up instance of command would be to execute TwainDotNet.Twain.StartScanning(). And so on…

My issue is that the TwainDotNet.WinFroms.WinFormsWindowMessageHook.PrefilterMessage() method is not ever executed. The scanner behavior at this point is that the motor starts up as if to engage the document feeder. However, the actual scan behavior never starts up. The scanner holds in this state for a few seconds and then the motor shuts off and goes into an idle state. Additionally, the MDI app is in a non-responsive state (maybe because the ImageHasTransferred/ScanningComplete events are not posted).

Suspect that this behavior is because TwainDotNet.Twain.StartScanning() is executed on my BGthread -not on my MDI app thread. When I remove the threading, things are working successfully.

Very grateful for any help, suggestions and especially answers!

jeske commented 7 years ago

You could try using the "newer" method of registering directly with TWAIN to receive a callback, instead of hooking the message pump. See page (2-9) of the twain spec, under the heading Using DAT_CALLBACK for Messages from the Source to the Application.