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;
@@ -41,24 +41,27 @@ namespace Tester.WPF
protected override void OnTransferError(TransferErrorEventArgs e) protected override void OnTransferError(TransferErrorEventArgs e)
{ {
if (e.Exception != null) App.Current.Dispatcher.BeginInvoke(new Action(() =>
{ {
Messenger.Default.Send(new DialogMessage(e.Exception.Message, null) if (e.Exception != null)
{ {
Caption = "Transfer Error Exception", Messenger.Default.Send(new DialogMessage(e.Exception.Message, null)
Icon = System.Windows.MessageBoxImage.Error, {
Button = System.Windows.MessageBoxButton.OK Caption = "Transfer Error Exception",
}); Icon = System.Windows.MessageBoxImage.Error,
} Button = System.Windows.MessageBoxButton.OK
else });
{ }
Messenger.Default.Send(new DialogMessage(string.Format("Return Code: {0}\nCondition Code: {1}", e.ReturnCode, e.SourceStatus.ConditionCode), null) else
{ {
Caption = "Transfer Error", Messenger.Default.Send(new DialogMessage(string.Format("Return Code: {0}\nCondition Code: {1}", e.ReturnCode, e.SourceStatus.ConditionCode), null)
Icon = System.Windows.MessageBoxImage.Error, {
Button = System.Windows.MessageBoxButton.OK Caption = "Transfer Error",
}); Icon = System.Windows.MessageBoxImage.Error,
} 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));
Image = img; }
if (img != null)
{
if (img.CanFreeze)
{
img.Freeze();
}
App.Current.Dispatcher.BeginInvoke(new Action(() =>
{
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 (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
if (e.NativeData != IntPtr.Zero) if (e.NativeData != IntPtr.Zero)
{ {
var img = e.NativeData.GetDrawingBitmap(); img = e.NativeData.GetDrawingBitmap();
if (img != null)
pictureBox1.Image = img;
} }
else if (!string.IsNullOrEmpty(e.FileDataPath)) else if (!string.IsNullOrEmpty(e.FileDataPath))
{ {
var img = new Bitmap(e.FileDataPath); img = new Bitmap(e.FileDataPath);
pictureBox1.Image = img; }
if (img != null)
{
this.BeginInvoke(new Action(() =>
{
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
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) =>
{ {