I have a C# program that uses TwainDotNet to scan and receive images from a fujitsu scanner. I have working code, but I want to perform scanning and image transfer asynchronously because the GUI hangs until the scan process is complete.
I attempted to follow the guidelines from this post's accepted solution using this code:
public void StartScanning()
{
// Run the scanner from a separate thread
Task.Run(() => ScanThread());
}
private void ScanThread()
{
// Instantiate the Twain object and hookup event handlers
Twain twain = new Twain(new WinFormsWindowMessageHook(new Form()));
twain.TransferImage += Twain_TransferImage;
twain.ScanningComplete += Twain_ScanningComplete;
// Start the scanning process by passing along pre-defined scan settings
twain.StartScanning(GetScanSettings());
}
The code inside ScanThread()
is technically correct, because it works outside of the call to Task.Run()
(meaning it runs fine on the GUI thread). However, ScanThread()
does not work if called from a new thread. The scanner hardware never begins to scan images, let alone transfer them.
Does anyone know of a concrete way to scan and transfer images asynchronously using TWAIN?