Added experimental option to sync events to UI thread.

This commit is contained in:
soukoku
2014-04-16 20:39:30 -04:00
parent b7993de123
commit a91d5aa4f2
6 changed files with 162 additions and 132 deletions

View File

@@ -11,6 +11,7 @@ using System.Windows.Media.Imaging;
using CommonWin32;
using System.Threading;
using GalaSoft.MvvmLight.Messaging;
using System.Diagnostics;
namespace Tester.WPF
{
@@ -22,7 +23,7 @@ namespace Tester.WPF
public TwainVM()
: base(TWIdentity.CreateFromAssembly(DataGroups.Image | DataGroups.Audio, Assembly.GetEntryAssembly()))
{
this.SynchronizationContext = SynchronizationContext.Current;
}
private ImageSource _image;
@@ -63,7 +64,6 @@ namespace Tester.WPF
Button = System.Windows.MessageBoxButton.OK
});
}
base.OnTransferError(e);
}
protected override void OnTransferReady(TransferReadyEventArgs e)
@@ -82,24 +82,22 @@ namespace Tester.WPF
};
var rc = this.DGControl.SetupFileXfer.Set(fileSetup);
}
base.OnTransferReady(e);
}
protected override void OnDataTransferred(DataTransferredEventArgs e)
{
App.Current.Dispatcher.Invoke(new Action(() =>
//App.Current.Dispatcher.Invoke(new Action(() =>
//{
if (e.NativeData != IntPtr.Zero)
{
if (e.NativeData != IntPtr.Zero)
{
Image = e.NativeData.GetWPFBitmap();
}
else if (!string.IsNullOrEmpty(e.FileDataPath))
{
var img = new BitmapImage(new Uri(e.FileDataPath));
Image = img;
}
}));
base.OnDataTransferred(e);
Image = e.NativeData.GetWPFBitmap();
}
else if (!string.IsNullOrEmpty(e.FileDataPath))
{
var img = new BitmapImage(new Uri(e.FileDataPath));
Image = img;
}
//}));
}
public void TestCapture(IntPtr hwnd)

View File

@@ -61,10 +61,21 @@ namespace Tester.Winform
{
var appId = TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetEntryAssembly());
_twain = new TwainSession(appId);
// either set this and don't worry about threads during events,
// or don't and invoke during the events yourselv
_twain.SynchronizationContext = SynchronizationContext.Current;
_twain.StateChanged += (s, e) =>
{
Debug.WriteLine("State change on thread " + Thread.CurrentThread.ManagedThreadId);
//this.BeginInvoke(new Action(() =>
//{
// Debug.WriteLine("State change marshaled to thread " + Thread.CurrentThread.ManagedThreadId);
//}));
};
_twain.DataTransferred += (s, e) =>
{
this.Invoke(new Action(() =>
{
//this.Invoke(new Action(() =>
//{
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
@@ -72,7 +83,6 @@ namespace Tester.Winform
}
if (e.NativeData != IntPtr.Zero)
{
//_ptrTest = e.Data;
var img = e.NativeData.GetDrawingBitmap();
if (img != null)
pictureBox1.Image = img;
@@ -82,17 +92,17 @@ namespace Tester.Winform
var img = new Bitmap(e.FileDataPath);
pictureBox1.Image = img;
}
}));
//}));
};
_twain.SourceDisabled += (s, e) =>
{
this.Invoke(new Action(() =>
{
//this.Invoke(new Action(() =>
//{
btnStopScan.Enabled = false;
btnStartCapture.Enabled = true;
panelOptions.Enabled = true;
LoadSourceCaps();
}));
//}));
};
_twain.TransferReady += (s, e) =>
{