1
0
mirror of https://github.com/soukoku/ntwain.git synced 2025-04-05 20:59:23 +08:00

Updated comments on sync context.

This commit is contained in:
soukoku 2014-05-14 19:58:45 -04:00
parent fb427437d7
commit ee2cbfa5e4
4 changed files with 28 additions and 25 deletions
NTwain
README.md
Tests/Tester.Winform

View File

@ -1,6 +1,7 @@
using NTwain.Data; using NTwain.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;

View File

@ -564,6 +564,8 @@ namespace NTwain
var syncer = SynchronizationContext; var syncer = SynchronizationContext;
if (syncer == null) if (syncer == null)
{ {
Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Trying to raise event {0} on thread {1} without sync.", e.GetType().Name, Thread.CurrentThread.ManagedThreadId));
try try
{ {
onEventFunc(e); onEventFunc(e);
@ -573,6 +575,8 @@ namespace NTwain
} }
else else
{ {
Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Trying to raise event {0} on thread {1} with sync.", e.GetType().Name, Thread.CurrentThread.ManagedThreadId));
// on some consumer desktop scanner with poor drivers this can frequently hang. there's nothing I can do here.
syncer.Send(o => syncer.Send(o =>
{ {
try try

View File

@ -76,7 +76,9 @@ example for handling DIB image in native transfer using the CommonWin32 lib.
Because it hosts its own message thread, the events will likely be raised from another thread. Because it hosts its own message thread, the events will likely be raised from another thread.
If you would like things marshalled to a UI thread then set the SynchronizationContext property If you would like things marshalled to a UI thread then set the SynchronizationContext property
to the one from the UI thread. to the one from the UI thread. Note that on certain consumer-grade scanner drivers this may hang the
event, so if you find yourself in that position you'll have to find another way
to synchronize data to UI threads.
``` ```

View File

@ -61,42 +61,38 @@ namespace Tester.Winform
_twain.SynchronizationContext = SynchronizationContext.Current; _twain.SynchronizationContext = SynchronizationContext.Current;
_twain.StateChanged += (s, e) => _twain.StateChanged += (s, e) =>
{ {
Debug.WriteLine("State change on thread " + Thread.CurrentThread.ManagedThreadId); Debug.WriteLine("State changed to " + _twain.State + " on thread " + Thread.CurrentThread.ManagedThreadId);
//this.BeginInvoke(new Action(() =>
//{
// Debug.WriteLine("State change marshaled to thread " + Thread.CurrentThread.ManagedThreadId);
//}));
}; };
_twain.DataTransferred += (s, e) => _twain.DataTransferred += (s, e) =>
{ {
//this.Invoke(new Action(() => //this.Invoke(new Action(() =>
//{ //{
if (pictureBox1.Image != null) if (pictureBox1.Image != null)
{ {
pictureBox1.Image.Dispose(); pictureBox1.Image.Dispose();
pictureBox1.Image = null; pictureBox1.Image = null;
} }
if (e.NativeData != IntPtr.Zero) if (e.NativeData != IntPtr.Zero)
{ {
var img = e.NativeData.GetDrawingBitmap(); var img = e.NativeData.GetDrawingBitmap();
if (img != null) if (img != null)
pictureBox1.Image = img;
}
else if (!string.IsNullOrEmpty(e.FileDataPath))
{
var img = new Bitmap(e.FileDataPath);
pictureBox1.Image = img; pictureBox1.Image = img;
} }
else if (!string.IsNullOrEmpty(e.FileDataPath))
{
var img = new Bitmap(e.FileDataPath);
pictureBox1.Image = img;
}
//})); //}));
}; };
_twain.SourceDisabled += (s, e) => _twain.SourceDisabled += (s, e) =>
{ {
//this.Invoke(new Action(() => //this.Invoke(new Action(() =>
//{ //{
btnStopScan.Enabled = false; btnStopScan.Enabled = false;
btnStartCapture.Enabled = true; btnStartCapture.Enabled = true;
panelOptions.Enabled = true; panelOptions.Enabled = true;
LoadSourceCaps(); LoadSourceCaps();
//})); //}));
}; };
_twain.TransferReady += (s, e) => _twain.TransferReady += (s, e) =>