Tattakashi123 / t

Automatically exported from code.google.com/p/twaindotnet
0 stars 0 forks source link

interaction with the scanner on a separate thread from the windows form #62

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Main form uses an interface class to start a background thread.
2. The background thread is monitoring a queue of commands from the main form.
3. The background thread is able to open the twain data source manager and 
twain data source, but hangs when acquiring images from a scanner

What is the expected output? What do you see instead?
When performed in the same thread as the main form, the image acquisition is 
successful.  When performed in a background thread, the image acquisition seems 
to hang, as if the data source is waiting on windows messages?

What version of the product are you using? On what operating system?
There doesn't seem to be a version number for this codebase, but I pulled the 
source about 3 weeks ago.  My application is running on Win7

Please provide any additional information below.
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.

So basically, I have a form app that starts a background thread (BGthread).  
The BGthread creates a 
System.Collection.Concurrent.BlockingCollection<MyAppMsg> and processes the 
<MyAppMsg> that get added to the collection.  This BGthread uses the 
TwainDotNet classes to interact with the downstream scanner.

My MDI app puts a <MyAppMsg> into the collection monitored by the BGthread.  
For example, an instance of <MyAppMsg> would be a command to instantiate the 
TwainDotNet.Twain class.  A follow-up instance of <MyAppMsg> would be a command 
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.  This has been tested on multiple scanners (Kodak, Canon, HP) and 
the behavior is the same.

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).  

 I 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.

Can you please help me to run TwainDotNet from a thread? I am grateful for any 
suggestions/comments that you can provide. 

Original issue reported on code.google.com by mree.oso...@gmail.com on 31 May 2013 at 3:47

GoogleCodeExporter commented 8 years ago
This is an issue that goes all the way down to a Twain_32.dll level. The twain 
specification expects to have a window handle so that it can send messages to 
the host application, including but not limited to Transferring Images, 
Initializing the Scan process, and Showing Modal Dialogs to automate the 
process. Without this window handle, you can't use a Twain device. The 
workaround I've found (in WPF, but there's an alias in WinForms) is to create a 
window class specifically to handle Twain, create that window in its own thread 
and give it its own message pump. When the process you want to complete is 
finished, then close the window. If you Join this thread immediately after 
starting it, then the process will block and behave as if you just asked the 
Twain device to scan. The downside is that you will now have a weird window 
just popping up when you need to scan, but it's better than having to convolute 
your main UI thread and event processing.

Original comment by specialf...@gmail.com on 7 Feb 2014 at 1:42