Initial internal loop idea that compiles.

This commit is contained in:
soukoku
2014-04-14 19:04:48 -04:00
parent 04ee0baf75
commit 320dfb2175
13 changed files with 231 additions and 611 deletions

View File

@@ -12,7 +12,7 @@ namespace NTwain.Tests
[ExpectedException(typeof(TwainStateException), "State check failed to throw.")]
public void VerifyState_Throws_When_State_Is_Enforced()
{
ITwainStateInternal session = new TwainSessionOld(TWIdentity.Create(DataGroups.Image, new Version(1, 0), "test", "test", "test", "test"));
ITwainStateInternal session = new TwainSession(TWIdentity.Create(DataGroups.Image, new Version(1, 0), "test", "test", "test", "test"));
session.EnforceState = true;
session.ChangeState(4, false);
@@ -22,7 +22,7 @@ namespace NTwain.Tests
[TestMethod]
public void VerifyState_No_Throws_When_State_Is_Not_Enforced()
{
ITwainStateInternal session = new TwainSessionOld(TWIdentity.Create(DataGroups.Image, new Version(1, 0), "test", "test", "test", "test"));
ITwainStateInternal session = new TwainSession(TWIdentity.Create(DataGroups.Image, new Version(1, 0), "test", "test", "test", "test"));
session.EnforceState = false;
session.ChangeState(4, false);

View File

@@ -17,14 +17,9 @@ namespace Tester
{
static void Main(string[] args)
{
// just an amusing example to do twain in console without UI, but may not work in real life
Console.WriteLine("Running Main on thread {0}", Thread.CurrentThread.ManagedThreadId);
new Thread(new ParameterizedThreadStart(DoTwainWork)).Start(Dispatcher.CurrentDispatcher);
// basically just needs a msg loop to act as the UI thread
Dispatcher.Run();
Console.WriteLine("Test completed.");
// just an amusing example to do twain in console without UI
DoTwainWork();
Console.WriteLine("Test completed, press Enter to exit.");
Console.ReadLine();
}
@@ -41,19 +36,12 @@ namespace Tester
}
static void DoTwainWork(object obj)
static void DoTwainWork()
{
Console.WriteLine("Getting ready to do twain stuff on thread {0}", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
var mySyncer = SynchronizationContext.Current;
if (mySyncer == null)
{
mySyncer = new DispatcherSynchronizationContext(obj as Dispatcher);
}
var rc = twain.OpenManager(IntPtr.Zero);
var rc = twain.OpenManager();
if (rc == ReturnCode.Success)
{
@@ -61,20 +49,13 @@ namespace Tester
if (rc == ReturnCode.Success)
{
// enablesource must be on the thread the sync context works on
mySyncer.Post(blah =>
{
rc = twain.EnableSource(SourceEnableMode.NoUI, false, IntPtr.Zero, blah as SynchronizationContext);
}, mySyncer);
return;
rc = twain.EnableSource(SourceEnableMode.NoUI, false, IntPtr.Zero);
}
else
{
twain.CloseManager();
}
}
Dispatcher.ExitAllFrames();
}
static void twain_SourceDisabled(object sender, EventArgs e)
@@ -82,8 +63,6 @@ namespace Tester
Console.WriteLine("Source disabled on thread {0}", Thread.CurrentThread.ManagedThreadId);
var rc = twain.CloseSource();
rc = twain.CloseManager();
Dispatcher.ExitAllFrames();
}
static void twain_TransferReady(object sender, TransferReadyEventArgs e)

View File

@@ -68,12 +68,7 @@ namespace Tester.WPF
{
base.OnSourceInitialized(e);
var hwnd = new WindowInteropHelper(this).Handle;
// this line is unnecessary if using twain 2 dsm but doesn't hurt to use it
HwndSource.FromHwnd(hwnd).AddHook(_twainVM.PreFilterMessage);
var rc = _twainVM.OpenManager(hwnd);
var rc = _twainVM.OpenManager();
if (rc == ReturnCode.Success)
{
SrcList.ItemsSource = _twainVM.GetSources().Select(s => new DSVM { DS = s });

View File

@@ -17,7 +17,7 @@ namespace Tester.WPF
/// <summary>
/// Wraps the twain session as a view model for databinding.
/// </summary>
class TwainVM : TwainSessionWPF
class TwainVM : TwainSession
{
public TwainVM()
: base(TWIdentity.CreateFromAssembly(DataGroups.Image | DataGroups.Audio, Assembly.GetEntryAssembly()))
@@ -87,15 +87,18 @@ namespace Tester.WPF
protected override void OnDataTransferred(DataTransferredEventArgs e)
{
if (e.NativeData != IntPtr.Zero)
App.Current.Dispatcher.Invoke(new Action(() =>
{
Image = e.NativeData.GetWPFBitmap();
}
else if (!string.IsNullOrEmpty(e.FileDataPath))
{
var img = new BitmapImage(new Uri(e.FileDataPath));
Image = img;
}
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);
}
@@ -113,7 +116,7 @@ namespace Tester.WPF
this.CapSetImageXferMech(XferMech.File);
}
var rc = EnableSource(SourceEnableMode.NoUI, false, hwnd, SynchronizationContext.Current);
var rc = EnableSource(SourceEnableMode.NoUI, false, hwnd);
}
}
}

View File

@@ -20,7 +20,7 @@ namespace Tester.Winform
sealed partial class TestForm : Form
{
ImageCodecInfo _tiffCodecInfo;
TwainSessionWinform _twain;
TwainSession _twain;
bool _stopScan;
bool _loadingCaps;
@@ -61,44 +61,48 @@ namespace Tester.Winform
private void SetupTwain()
{
var appId = TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetEntryAssembly());
_twain = new TwainSessionWinform(appId);
_twain = new TwainSession(appId);
_twain.DataTransferred += (s, e) =>
{
if (pictureBox1.Image != null)
this.Invoke(new Action(() =>
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
if (e.NativeData != IntPtr.Zero)
{
//_ptrTest = e.Data;
var img = e.NativeData.GetDrawingBitmap();
if (img != null)
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
if (e.NativeData != IntPtr.Zero)
{
//_ptrTest = e.Data;
var img = e.NativeData.GetDrawingBitmap();
if (img != null)
pictureBox1.Image = img;
}
else if (!string.IsNullOrEmpty(e.FileDataPath))
{
var img = new Bitmap(e.FileDataPath);
pictureBox1.Image = img;
}
else if (!string.IsNullOrEmpty(e.FileDataPath))
{
var img = new Bitmap(e.FileDataPath);
pictureBox1.Image = img;
}
}
}));
};
_twain.SourceDisabled += (s, e) =>
{
btnStopScan.Enabled = false;
btnStartCapture.Enabled = true;
panelOptions.Enabled = true;
LoadSourceCaps();
this.Invoke(new Action(() =>
{
btnStopScan.Enabled = false;
btnStartCapture.Enabled = true;
panelOptions.Enabled = true;
LoadSourceCaps();
}));
};
_twain.TransferReady += (s, e) =>
{
e.CancelAll = _stopScan;
};
Application.AddMessageFilter(_twain);
}
private void CleanupTwain()
{
Application.RemoveMessageFilter(_twain);
if (_twain.State == 4)
{
_twain.CloseSource();
@@ -164,7 +168,7 @@ namespace Tester.Winform
if (_twain.SupportedCaps.Contains(CapabilityId.CapUIControllable))
{
// hide scanner ui if possible
if (_twain.EnableSource(SourceEnableMode.NoUI, false, this.Handle, SynchronizationContext.Current) == ReturnCode.Success)
if (_twain.EnableSource(SourceEnableMode.NoUI, false, this.Handle) == ReturnCode.Success)
{
btnStopScan.Enabled = true;
btnStartCapture.Enabled = false;
@@ -173,7 +177,7 @@ namespace Tester.Winform
}
else
{
if (_twain.EnableSource(SourceEnableMode.ShowUI, true, this.Handle, SynchronizationContext.Current) == ReturnCode.Success)
if (_twain.EnableSource(SourceEnableMode.ShowUI, true, this.Handle) == ReturnCode.Success)
{
btnStopScan.Enabled = true;
btnStartCapture.Enabled = false;
@@ -229,7 +233,7 @@ namespace Tester.Winform
{
if (_twain.State < 3)
{
_twain.OpenManager(this.Handle);
_twain.OpenManager();
}
if (_twain.State >= 3)
@@ -356,7 +360,7 @@ namespace Tester.Winform
private void btnAllSettings_Click(object sender, EventArgs e)
{
_twain.EnableSource(SourceEnableMode.ShowUIOnly, true, this.Handle, SynchronizationContext.Current);
_twain.EnableSource(SourceEnableMode.ShowUIOnly, true, this.Handle);
}
#endregion