How can I scan and transfer images from a document feeder asynchronously

萝らか妹 提交于 2019-12-04 07:23:52

I work for Atalasoft, but I don't know WPF, or even that much about DotTwain!

I can tell you that generally TWAIN scanning can be done on a separate scanning thread, but you have to take some care. The simplest approach is to do all TWAIN operations on the scanning thread - don't mix TWAIN calls between two threads.

The scanning thread has to have a message pump or be a 'UI' thread, whatever that takes in your environment. It is not just a worker thread.

TWAIN expects to be given a window handle (old-fashioned Win32 HWND) to use as the parent window for the scanner's UI. I recommend creating a 'scanning parent' window for this purpose, on the scanning thread. You can make it visible or not as you choose, and destroy it at the end of the scan job.

If your scan jobs can be very big (e.g. 50 pages of 400 DPI color) you have to make sure the scanning process does not fill up either logical memory or RAM. If you fill up logical memory (a 32-bit Windows process gets about 2GB of address space to work with) allocations will fail. If you fill up RAM, the code that is consuming/disposing of the incoming images may start to swap, slowing down radically, then scanning runs ahead and fills up logical memory. So you need to either:

  1. Completely process and dispose of each incoming image on the scan thread, or
  2. Throttle the flow of images from the scan thread so it cannot run too far ahead of their processing/disposition.

I usually find I want to be able to cancel the scan thread, which requires some patience since TWAIN calls cannot be interrupted, and some of them are heavy. As you've noticed with your Canon. On the other hand if you force-kill a thread inside a TWAIN call, the scanner may require a power-cycle or even a system restart, and TWAIN itself will block until the TWAIN manager DLL is unloaded from memory and reloaded. Usually best to shut down TWAIN very politely.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!