I have a problem which is causing me some headaches. I have a Winform application which shows a very simple dialog to users with just a button: "SCAN". The button's click handler, how could you easily imagine, starts a scan operation using the WIA COM object. In terms of code, when the button has pressed it creates a new async Task which calls the Transfer method of the Item class. The problem is that the UI thread freezes itself until the scan ends, although the scan object is working on another thread. The documentation on MSDN says:
"Transfer is essentially a version of ShowTransfer that does not display a UI or allow the user to cancel the transfer."
So I assumed that, with the sentence "not display a UI", it means that the dialog gets created anyway but it remains invisible. This dialog then, hooks itself to the main thread (UI) and causes the freeze (in the same way a modal dialog does). In fact when I've tried to use the ShowTransfer
method it has opened its dialog but the UI thread of the application didn't suffer the freeze.
This is my code:
var progress = new Progress<string>(s => labelControl1.Text = s);
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
var cancelSrc = new CancellationTokenSource();
await Task.Factory.StartNew(() => {
var facade = new ScanFacade();
facade.Scan(progress);
}, cancelSrc.Token, TaskCreationOptions.LongRunning, scheduler);
And this is the part of the Scan method which calls Transfer:
var imageFile = (ImageFile) _item.Transfer(FormatID.wiaFormatTIFF);
var buffer = (byte[])imageFile.FileData.get_BinaryData();
return Image.FromStream(new MemoryStream(buffer)) as Bitmap;
Are my assumptions correct? How can I avoid this behavior without using the WIA dialog?
Note: threads are both in STA
Thank you very much in advance