Updated samples to not use sync context as well.

This commit is contained in:
soukoku
2014-05-14 20:13:29 -04:00
parent ee2cbfa5e4
commit 1cd21f86cb
2 changed files with 56 additions and 40 deletions

View File

@@ -18,7 +18,7 @@ namespace Tester.WPF
public TwainVM() public TwainVM()
: base(TWIdentity.CreateFromAssembly(DataGroups.Image | DataGroups.Audio, Assembly.GetEntryAssembly())) : base(TWIdentity.CreateFromAssembly(DataGroups.Image | DataGroups.Audio, Assembly.GetEntryAssembly()))
{ {
this.SynchronizationContext = SynchronizationContext.Current; //this.SynchronizationContext = SynchronizationContext.Current;
} }
private ImageSource _image; private ImageSource _image;
@@ -40,6 +40,8 @@ namespace Tester.WPF
} }
protected override void OnTransferError(TransferErrorEventArgs e) protected override void OnTransferError(TransferErrorEventArgs e)
{
App.Current.Dispatcher.BeginInvoke(new Action(() =>
{ {
if (e.Exception != null) if (e.Exception != null)
{ {
@@ -59,6 +61,7 @@ namespace Tester.WPF
Button = System.Windows.MessageBoxButton.OK Button = System.Windows.MessageBoxButton.OK
}); });
} }
}));
} }
protected override void OnTransferReady(TransferReadyEventArgs e) protected override void OnTransferReady(TransferReadyEventArgs e)
@@ -81,14 +84,25 @@ namespace Tester.WPF
protected override void OnDataTransferred(DataTransferredEventArgs e) protected override void OnDataTransferred(DataTransferredEventArgs e)
{ {
ImageSource img = null;
if (e.NativeData != IntPtr.Zero) if (e.NativeData != IntPtr.Zero)
{ {
Image = e.NativeData.GetWPFBitmap(); img = e.NativeData.GetWPFBitmap();
} }
else if (!string.IsNullOrEmpty(e.FileDataPath)) else if (!string.IsNullOrEmpty(e.FileDataPath))
{ {
var img = new BitmapImage(new Uri(e.FileDataPath)); img = new BitmapImage(new Uri(e.FileDataPath));
}
if (img != null)
{
if (img.CanFreeze)
{
img.Freeze();
}
App.Current.Dispatcher.BeginInvoke(new Action(() =>
{
Image = img; Image = img;
}));
} }
} }

View File

@@ -58,42 +58,44 @@ namespace Tester.Winform
_twain = new TwainSession(appId); _twain = new TwainSession(appId);
// either set this and don't worry about threads during events, // either set this and don't worry about threads during events,
// or don't and invoke during the events yourself // or don't and invoke during the events yourself
_twain.SynchronizationContext = SynchronizationContext.Current; //_twain.SynchronizationContext = SynchronizationContext.Current;
_twain.StateChanged += (s, e) => _twain.StateChanged += (s, e) =>
{ {
Debug.WriteLine("State changed to " + _twain.State + " on thread " + Thread.CurrentThread.ManagedThreadId); Debug.WriteLine("State changed to " + _twain.State + " on thread " + Thread.CurrentThread.ManagedThreadId);
}; };
_twain.DataTransferred += (s, e) => _twain.DataTransferred += (s, e) =>
{ {
//this.Invoke(new Action(() => Bitmap img = null;
//{ if (e.NativeData != IntPtr.Zero)
{
img = e.NativeData.GetDrawingBitmap();
}
else if (!string.IsNullOrEmpty(e.FileDataPath))
{
img = new Bitmap(e.FileDataPath);
}
if (img != null)
{
this.BeginInvoke(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)
{
var img = e.NativeData.GetDrawingBitmap();
if (img != null)
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.BeginInvoke(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) =>
{ {