Updated sample console to end xfer correctly.

This commit is contained in:
Eugene Wang
2021-04-22 07:29:03 -04:00
parent a29bb5ce8e
commit 5ca9bb601c

View File

@@ -12,19 +12,23 @@ namespace Net5Console
{
Console.WriteLine("Starting twain test in console...");
using (var twain = new TwainSession(Assembly.GetExecutingAssembly(), null, IntPtr.Zero))
using (var session = new TwainSession(Assembly.GetExecutingAssembly(), null, IntPtr.Zero))
using (var hold = new ManualResetEventSlim())
{
twain.DeviceEvent += (sender, e) =>
session.DeviceEvent += (sender, e) =>
{
Console.WriteLine($"Got device event " + (TWDE)e.Event);
};
twain.ScanEvent += (sender, closing) =>
session.ScanEvent += (sender, closing) =>
{
Console.WriteLine($"Got scan event " + closing);
// don't care, just end it
TW_PENDINGXFERS pending = default;
var sts = session.TWAIN.DatPendingxfers(DG.CONTROL, MSG.RESET, ref pending);
TW_USERINTERFACE twuserinterface = default;
if (twain.TWAIN.DatUserinterface(DG.CONTROL, MSG.DISABLEDS, ref twuserinterface) == STS.SUCCESS)
if (session.TWAIN.DatUserinterface(DG.CONTROL, MSG.DISABLEDS, ref twuserinterface) == STS.SUCCESS)
{
Console.WriteLine("Disabled device.");
}
@@ -35,16 +39,16 @@ namespace Net5Console
hold.Set();
};
if (twain.Open() == TWAINWorkingGroup.STS.SUCCESS)
if (session.Open() == TWAINWorkingGroup.STS.SUCCESS)
{
Console.WriteLine("Opened DSM");
Console.WriteLine("Default device:");
Console.WriteLine($"\t{twain.DefaultDevice}");
Console.WriteLine($"\t{session.DefaultDevice}");
Console.WriteLine("All devices:");
TW_IDENTITY dsToUse = default;
foreach (var dev in twain.GetDevices())
foreach (var dev in session.GetDevices())
{
Console.WriteLine($"\t{dev}");
if (dev.ProductName == "TWAIN2 FreeImage Software Scanner")
@@ -53,13 +57,13 @@ namespace Net5Console
}
}
twain.CurrentDevice = dsToUse;
if (twain.CurrentDevice.HasValue)
session.CurrentDevice = dsToUse;
if (session.CurrentDevice.HasValue)
{
Console.WriteLine("Current device after opening attempt:");
Console.WriteLine($"\t{twain.CurrentDevice}");
Console.WriteLine($"\t{session.CurrentDevice}");
var sts = twain.StartCapture(false);
var sts = session.StartCapture(false);
if (sts == STS.SUCCESS)
{
Console.Error.WriteLine("Waiting for capture to complete.");