Fixed creating/opening/closing twainsession multiple times in an app not working.

This commit is contained in:
soukoku
2014-08-14 07:34:55 -04:00
parent a8891efe3b
commit f073bc6b65
13 changed files with 147 additions and 53 deletions

View File

@@ -1,7 +1,7 @@
<Application x:Class="Tester.WPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
StartupUri="Dummy.xaml">
<Application.Resources>
<ResourceDictionary>
<!-- Dummy Style, anything you won't use goes -->

View File

@@ -0,0 +1,12 @@
<Window x:Class="Tester.WPF.Dummy"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Dummy" Height="300" Width="300">
<Grid>
<StackPanel VerticalAlignment="Center">
<Button Content="Open scan window" Click="Button_Click" Margin="4 0"></Button>
<TextBlock Text="This is to test opening/closing multiple twain sessions in an app" Margin="8"
TextWrapping="Wrap"></TextBlock>
</StackPanel>
</Grid>
</Window>

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Tester.WPF
{
/// <summary>
/// Interaction logic for Dummy.xaml
/// </summary>
public partial class Dummy : Window
{
public Dummy()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
new MainWindow().ShowDialog();
}
}
}

View File

@@ -4,6 +4,7 @@ using NTwain;
using NTwain.Data;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
@@ -51,13 +52,18 @@ namespace Tester.WPF
});
}
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = _twainVM.State > 4;
base.OnClosing(e);
}
protected override void OnClosed(EventArgs e)
{
if (_twainVM.State == 4)
{
_twainVM.CurrentSource.Close();
}
_twainVM.Close();
base.OnClosed(e);
}
@@ -68,6 +74,8 @@ namespace Tester.WPF
// use this for internal msg loop
//var rc = _twainVM.Open();
// use this to hook into current app loop
var rc = _twainVM.Open(new WpfMessageLoopHook(new WindowInteropHelper(this).Handle));
if (rc == ReturnCode.Success)
@@ -82,6 +90,15 @@ namespace Tester.WPF
private void SrcList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//var test = new NTwain.Internals.InternalMessageLoopHook();
//test.StartTest();
//test.BeginInvoke(() =>
//{
// Debug.WriteLine("doodle");
// test.StopTest();
//});
if (_twainVM.State == 4)
{
_twainVM.CurrentSource.Close();

View File

@@ -80,7 +80,14 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Dummy.xaml.cs">
<DependentUpon>Dummy.xaml</DependentUpon>
</Compile>
<Compile Include="ViewModels\TwainVM.cs" />
<Page Include="Dummy.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>

View File

@@ -3,6 +3,7 @@ using GalaSoft.MvvmLight.Messaging;
using NTwain;
using NTwain.Data;
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Media;
@@ -76,12 +77,23 @@ namespace Tester.WPF
var fileSetup = new TWSetupFileXfer
{
Format = wantFormat,
FileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.tif")
FileName = GetUniqueName(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test", ".tif")
};
var rc = this.CurrentSource.DGControl.SetupFileXfer.Set(fileSetup);
}
}
private string GetUniqueName(string dir, string name, string ext)
{
var filePath = Path.Combine(dir, name + ext);
int next = 1;
while (File.Exists(filePath))
{
filePath = Path.Combine(dir, string.Format("{0} ({1}){2}", name, next++, ext));
}
return filePath;
}
protected override void OnDataTransferred(DataTransferredEventArgs e)
{
ImageSource img = null;