Added musings on runnig as Console app.

This commit is contained in:
soukoku
2014-04-06 15:22:11 -04:00
parent 0bd58e5e48
commit 006d33b451
7 changed files with 272 additions and 5 deletions

View File

@@ -0,0 +1,106 @@
using NTwain;
using NTwain.Data;
using NTwain.Values;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Threading;
namespace Tester
{
class Program
{
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.");
Console.ReadLine();
}
static readonly TwainSession twain = InitTwain();
private static TwainSession InitTwain()
{
var twain = new TwainSession(TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetExecutingAssembly()));
twain.DataTransferred += twain_DataTransferred;
twain.TransferReady += twain_TransferReady;
twain.SourceDisabled += twain_SourceDisabled;
return twain;
}
static void DoTwainWork(object obj)
{
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(default(HandleRef));
if (rc == ReturnCode.Success)
{
rc = twain.OpenSource("TWAIN2 FreeImage Software Scanner");
if (rc == ReturnCode.Success)
{
// enablesource must be on the thread the sync context works on
mySyncer.Post(blah =>
{
rc = twain.EnableSource(SourceEnableMode.NoUI, false, default(HandleRef), blah as SynchronizationContext);
}, mySyncer);
return;
}
else
{
twain.CloseManager();
}
}
Dispatcher.ExitAllFrames();
}
static void twain_SourceDisabled(object sender, EventArgs e)
{
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)
{
Console.WriteLine("Got xfer ready on thread {0}", Thread.CurrentThread.ManagedThreadId);
}
static void twain_DataTransferred(object sender, DataTransferredEventArgs e)
{
if (e.Data != IntPtr.Zero)
{
Console.WriteLine("Got twain data on thread {0}", Thread.CurrentThread.ManagedThreadId);
}
else
{
Console.WriteLine("No twain data on thread {0}", Thread.CurrentThread.ManagedThreadId);
}
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Tester.Console")]
[assembly: AssemblyDescription("Console app for quick testing implementations in the TWAIN lib.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Yin-Chun Wang")]
[assembly: AssemblyProduct("Tester.Console")]
[assembly: AssemblyCopyright("Copyright © Yin-Chun Wang 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c27df82d-5be9-4396-8905-0af6ea6e63cd")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{12D761EF-68DF-41CE-92EF-0C7AE81857A3}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Tester</RootNamespace>
<AssemblyName>Tester.Console</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\NTwain\NTwain.csproj">
<Project>{0c5a6fb1-0282-4d61-8354-68deb1515001}</Project>
<Name>NTwain</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>