mirror of
https://github.com/soukoku/ntwain.git
synced 2025-11-08 02:24:46 +08:00
Fixed creating/opening/closing twainsession multiple times in an app not working.
This commit is contained in:
@@ -9,7 +9,7 @@ using System.Windows.Threading;
|
||||
|
||||
namespace NTwain.Internals
|
||||
{
|
||||
sealed class InternalMessageLoopHook : MessageLoopHook
|
||||
sealed class InternalMessageLoopHook : MessageLoopHook
|
||||
{
|
||||
Dispatcher _dispatcher;
|
||||
WindowsHook _hook;
|
||||
@@ -55,14 +55,14 @@ namespace NTwain.Internals
|
||||
}
|
||||
}
|
||||
|
||||
internal override void BeginInvoke(Action action)
|
||||
public override void BeginInvoke(Action action)
|
||||
{
|
||||
if (_dispatcher == null) { throw new InvalidOperationException(Resources.MsgLoopUnavailble); }
|
||||
|
||||
_dispatcher.BeginInvoke(DispatcherPriority.Normal, action);
|
||||
}
|
||||
|
||||
internal override void Invoke(Action action)
|
||||
public override void Invoke(Action action)
|
||||
{
|
||||
if (_dispatcher == null) { throw new InvalidOperationException(Resources.MsgLoopUnavailble); }
|
||||
|
||||
|
||||
@@ -16,7 +16,11 @@ namespace NTwain
|
||||
internal abstract void Start(IWinMessageFilter filter);
|
||||
internal abstract void Stop();
|
||||
|
||||
internal virtual void BeginInvoke(Action action)
|
||||
/// <summary>
|
||||
/// Asynchronously invokes the specified action on the message loop thread.
|
||||
/// </summary>
|
||||
/// <param name="action">The action.</param>
|
||||
public virtual void BeginInvoke(Action action)
|
||||
{
|
||||
if (SyncContext == null)
|
||||
{
|
||||
@@ -30,7 +34,12 @@ namespace NTwain
|
||||
}, null);
|
||||
}
|
||||
}
|
||||
internal virtual void Invoke(Action action)
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously invokes the specified action on the message loop thread.
|
||||
/// </summary>
|
||||
/// <param name="action">The action.</param>
|
||||
public virtual void Invoke(Action action)
|
||||
{
|
||||
if (SyncContext == null)
|
||||
{
|
||||
|
||||
@@ -14,6 +14,6 @@ namespace NTwain
|
||||
// keep this same in majors releases
|
||||
public const string Release = "2.0.0.0";
|
||||
// change this for each nuget release
|
||||
public const string Build = "2.0.4";
|
||||
public const string Build = "2.0.5";
|
||||
}
|
||||
}
|
||||
@@ -54,12 +54,18 @@ namespace NTwain
|
||||
|
||||
internal static TwainSource GetSourceInstance(ITwainSessionInternal session, TWIdentity sourceId)
|
||||
{
|
||||
TwainSource source = null;
|
||||
var key = string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}", sourceId.Manufacturer, sourceId.ProductFamily, sourceId.ProductName);
|
||||
if (__ownedSources.ContainsKey(key))
|
||||
{
|
||||
return __ownedSources[key];
|
||||
source = __ownedSources[key];
|
||||
source.Session = session;
|
||||
}
|
||||
return __ownedSources[key] = new TwainSource(session, sourceId);
|
||||
else
|
||||
{
|
||||
__ownedSources[key] = source = new TwainSource(session, sourceId);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace NTwain
|
||||
QuerySupport retVal = QuerySupport.None;
|
||||
using (TWCapability cap = new TWCapability(capId))
|
||||
{
|
||||
var rc = _session.DGControl.Capability.QuerySupport(cap);
|
||||
var rc = Session.DGControl.Capability.QuerySupport(cap);
|
||||
if (rc == ReturnCode.Success)
|
||||
{
|
||||
var read = CapabilityReader.ReadValue(cap);
|
||||
@@ -41,7 +41,7 @@ namespace NTwain
|
||||
{
|
||||
using (TWCapability cap = new TWCapability(capId))
|
||||
{
|
||||
var rc = _session.DGControl.Capability.GetCurrent(cap);
|
||||
var rc = Session.DGControl.Capability.GetCurrent(cap);
|
||||
if (rc == ReturnCode.Success)
|
||||
{
|
||||
var read = CapabilityReader.ReadValue(cap);
|
||||
@@ -82,7 +82,7 @@ namespace NTwain
|
||||
var list = new List<object>();
|
||||
using (TWCapability cap = new TWCapability(capabilityId))
|
||||
{
|
||||
var rc = _session.DGControl.Capability.Get(cap);
|
||||
var rc = Session.DGControl.Capability.Get(cap);
|
||||
if (rc == ReturnCode.Success)
|
||||
{
|
||||
cap.ReadMultiCapValues(list);
|
||||
@@ -126,7 +126,7 @@ namespace NTwain
|
||||
{
|
||||
using (TWCapability compressCap = new TWCapability(CapabilityId.ICapCompression, new TWOneValue { Item = (uint)compression, ItemType = ItemType.UInt16 }))
|
||||
{
|
||||
return _session.DGControl.Capability.Set(compressCap);
|
||||
return Session.DGControl.Capability.Set(compressCap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +153,7 @@ namespace NTwain
|
||||
{
|
||||
using (TWCapability formatCap = new TWCapability(CapabilityId.ICapImageFileFormat, new TWOneValue { Item = (uint)format, ItemType = ItemType.UInt16 }))
|
||||
{
|
||||
return _session.DGControl.Capability.Set(formatCap);
|
||||
return Session.DGControl.Capability.Set(formatCap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,7 +183,7 @@ namespace NTwain
|
||||
one.ItemType = ItemType.UInt16;
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.ICapPixelType, one))
|
||||
{
|
||||
return _session.DGControl.Capability.Set(dx);
|
||||
return Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ namespace NTwain
|
||||
one.ItemType = ItemType.UInt16;
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.ICapXferMech, one))
|
||||
{
|
||||
return _session.DGControl.Capability.Set(dx);
|
||||
return Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,7 +239,7 @@ namespace NTwain
|
||||
one.ItemType = ItemType.UInt16;
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.ACapXferMech, one))
|
||||
{
|
||||
return _session.DGControl.Capability.Set(dx);
|
||||
return Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,13 +282,13 @@ namespace NTwain
|
||||
|
||||
using (TWCapability xres = new TWCapability(CapabilityId.ICapXResolution, one))
|
||||
{
|
||||
var rc = _session.DGControl.Capability.Set(xres);
|
||||
var rc = Session.DGControl.Capability.Set(xres);
|
||||
if (rc == ReturnCode.Success)
|
||||
{
|
||||
one.Item = (uint)yDPI;
|
||||
using (TWCapability yres = new TWCapability(CapabilityId.ICapYResolution, one))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(yres);
|
||||
rc = Session.DGControl.Capability.Set(yres);
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
@@ -322,7 +322,7 @@ namespace NTwain
|
||||
|
||||
using (TWCapability xres = new TWCapability(CapabilityId.ICapSupportedSizes, one))
|
||||
{
|
||||
var rc = _session.DGControl.Capability.Set(xres);
|
||||
var rc = Session.DGControl.Capability.Set(xres);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
@@ -351,7 +351,7 @@ namespace NTwain
|
||||
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.ICapAutomaticDeskew, en))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(dx);
|
||||
rc = Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -362,7 +362,7 @@ namespace NTwain
|
||||
|
||||
using (TWCapability capValue = new TWCapability(CapabilityId.ICapAutomaticDeskew, one))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(capValue);
|
||||
rc = Session.DGControl.Capability.Set(capValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -389,7 +389,7 @@ namespace NTwain
|
||||
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.ICapAutomaticRotate, en))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(dx);
|
||||
rc = Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -400,7 +400,7 @@ namespace NTwain
|
||||
|
||||
using (TWCapability capValue = new TWCapability(CapabilityId.ICapAutomaticRotate, one))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(capValue);
|
||||
rc = Session.DGControl.Capability.Set(capValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -428,11 +428,11 @@ namespace NTwain
|
||||
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.ICapUndefinedImageSize, en))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(dx);
|
||||
rc = Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.ICapAutomaticBorderDetection, en))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(dx);
|
||||
rc = Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -443,11 +443,11 @@ namespace NTwain
|
||||
|
||||
using (TWCapability capValue = new TWCapability(CapabilityId.ICapUndefinedImageSize, one))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(capValue);
|
||||
rc = Session.DGControl.Capability.Set(capValue);
|
||||
}
|
||||
using (TWCapability capValue = new TWCapability(CapabilityId.ICapAutomaticBorderDetection, one))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(capValue);
|
||||
rc = Session.DGControl.Capability.Set(capValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -471,7 +471,7 @@ namespace NTwain
|
||||
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.CapDuplexEnabled, en))
|
||||
{
|
||||
return _session.DGControl.Capability.Set(dx);
|
||||
return Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -482,7 +482,7 @@ namespace NTwain
|
||||
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.CapDuplexEnabled, one))
|
||||
{
|
||||
return _session.DGControl.Capability.Set(dx);
|
||||
return Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -512,7 +512,7 @@ namespace NTwain
|
||||
{
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.CapFeederEnabled, en))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(dx);
|
||||
rc = Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -522,14 +522,14 @@ namespace NTwain
|
||||
{
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.CapAutoScan, en))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(dx);
|
||||
rc = Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
}
|
||||
else if (SupportedCaps.Contains(CapabilityId.CapAutoFeed))
|
||||
{
|
||||
using (TWCapability dx = new TWCapability(CapabilityId.CapAutoFeed, en))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(dx);
|
||||
rc = Session.DGControl.Capability.Set(dx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -543,7 +543,7 @@ namespace NTwain
|
||||
{
|
||||
using (TWCapability enabled = new TWCapability(CapabilityId.CapFeederEnabled, one))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(enabled);
|
||||
rc = Session.DGControl.Capability.Set(enabled);
|
||||
}
|
||||
}
|
||||
// to really use feeder we must also set autofeed or autoscan, but only
|
||||
@@ -552,14 +552,14 @@ namespace NTwain
|
||||
{
|
||||
using (TWCapability autoScan = new TWCapability(CapabilityId.CapAutoScan, one))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(autoScan);
|
||||
rc = Session.DGControl.Capability.Set(autoScan);
|
||||
}
|
||||
}
|
||||
else if (SupportedCaps.Contains(CapabilityId.CapAutoFeed))
|
||||
{
|
||||
using (TWCapability autoScan = new TWCapability(CapabilityId.CapAutoFeed, one))
|
||||
{
|
||||
rc = _session.DGControl.Capability.Set(autoScan);
|
||||
rc = Session.DGControl.Capability.Set(autoScan);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@ namespace NTwain
|
||||
/// </summary>
|
||||
public partial class TwainSource
|
||||
{
|
||||
ITwainSessionInternal _session;
|
||||
internal ITwainSessionInternal Session { get; set; }
|
||||
|
||||
internal TwainSource(ITwainSessionInternal session, TWIdentity sourceId)
|
||||
{
|
||||
_session = session;
|
||||
Session = session;
|
||||
Identity = sourceId;
|
||||
}
|
||||
|
||||
@@ -32,11 +32,11 @@ namespace NTwain
|
||||
public ReturnCode Open()
|
||||
{
|
||||
var rc = ReturnCode.Failure;
|
||||
_session.MessageLoopHook.Invoke(() =>
|
||||
Session.MessageLoopHook.Invoke(() =>
|
||||
{
|
||||
Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Thread {0}: OpenSource.", Thread.CurrentThread.ManagedThreadId));
|
||||
|
||||
rc = _session.DGControl.Identity.OpenDS(this);
|
||||
rc = Session.DGControl.Identity.OpenDS(this);
|
||||
});
|
||||
return rc;
|
||||
}
|
||||
@@ -48,11 +48,11 @@ namespace NTwain
|
||||
public ReturnCode Close()
|
||||
{
|
||||
var rc = ReturnCode.Failure;
|
||||
_session.MessageLoopHook.Invoke(() =>
|
||||
Session.MessageLoopHook.Invoke(() =>
|
||||
{
|
||||
Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Thread {0}: CloseSource.", Thread.CurrentThread.ManagedThreadId));
|
||||
|
||||
rc = _session.DGControl.Identity.CloseDS();
|
||||
rc = Session.DGControl.Identity.CloseDS();
|
||||
if (rc == ReturnCode.Success)
|
||||
{
|
||||
SupportedCaps = null;
|
||||
@@ -71,7 +71,7 @@ namespace NTwain
|
||||
/// <returns></returns>
|
||||
public ReturnCode StartTransfer(SourceEnableMode mode, bool modal, IntPtr windowHandle)
|
||||
{
|
||||
return _session.EnableSource(mode, modal, windowHandle);
|
||||
return Session.EnableSource(mode, modal, windowHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -81,7 +81,7 @@ namespace NTwain
|
||||
public TWStatus GetStatus()
|
||||
{
|
||||
TWStatus stat;
|
||||
_session.DGControl.Status.GetSource(out stat);
|
||||
Session.DGControl.Status.GetSource(out stat);
|
||||
return stat;
|
||||
}
|
||||
/// <summary>
|
||||
@@ -91,7 +91,7 @@ namespace NTwain
|
||||
public TWStatusUtf8 GetStatusUtf8()
|
||||
{
|
||||
TWStatusUtf8 stat;
|
||||
_session.DGControl.StatusUtf8.GetSource(out stat);
|
||||
Session.DGControl.StatusUtf8.GetSource(out stat);
|
||||
return stat;
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ namespace NTwain
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_supportedCaps == null && _session.State > 3)
|
||||
if (_supportedCaps == null && Session.State > 3)
|
||||
{
|
||||
_supportedCaps = CapGetValues(CapabilityId.CapSupportedCaps).CastToEnum<CapabilityId>(false);
|
||||
}
|
||||
@@ -191,7 +191,7 @@ namespace NTwain
|
||||
{
|
||||
get
|
||||
{
|
||||
return _session.DGControl;
|
||||
return Session.DGControl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ namespace NTwain
|
||||
{
|
||||
get
|
||||
{
|
||||
return _session.DGImage;
|
||||
return Session.DGImage;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,7 +213,7 @@ namespace NTwain
|
||||
{
|
||||
get
|
||||
{
|
||||
return _session.DGCustom;
|
||||
return Session.DGCustom;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ namespace NTwain
|
||||
///// <param name="propertyName">Name of the property.</param>
|
||||
//protected void OnPropertyChanged(string propertyName)
|
||||
//{
|
||||
// var syncer = _session.SynchronizationContext;
|
||||
// var syncer = Session.SynchronizationContext;
|
||||
// if (syncer == null)
|
||||
// {
|
||||
// try
|
||||
|
||||
Reference in New Issue
Block a user