-
-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix default TwainHandleManager implementation
- Loading branch information
Showing
6 changed files
with
97 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
NAPS2.Sdk/Scan/Internal/Twain/DefaultTwainHandleManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#if !MAC | ||
using System.Threading; | ||
using NAPS2.Platform.Windows; | ||
using NTwain; | ||
|
||
namespace NAPS2.Scan.Internal.Twain; | ||
|
||
/// <summary> | ||
/// TwainHandleManager implementation that lazily starts a Win32MessagePump and delegates to a Win32TwainHandleManager. | ||
/// </summary> | ||
[System.Runtime.Versioning.SupportedOSPlatform("windows")] | ||
internal class DefaultTwainHandleManager : TwainHandleManager | ||
{ | ||
private Win32MessagePump? _messagePump; | ||
private Win32TwainHandleManager? _inner; | ||
|
||
private Win32TwainHandleManager CreateInner() | ||
{ | ||
var mre = new ManualResetEvent(false); | ||
var messageThread = new Thread(() => | ||
{ | ||
_messagePump = Win32MessagePump.Create(); | ||
mre.Set(); | ||
_messagePump.RunMessageLoop(); | ||
}); | ||
messageThread.SetApartmentState(ApartmentState.STA); | ||
messageThread.Start(); | ||
mre.WaitOne(); | ||
return new Win32TwainHandleManager(_messagePump!); | ||
} | ||
|
||
public override IntPtr GetDsmHandle(IntPtr dialogParent, bool useNativeUi) | ||
{ | ||
_inner ??= CreateInner(); | ||
return _inner.GetDsmHandle(dialogParent, useNativeUi); | ||
} | ||
|
||
public override IntPtr GetEnableHandle(IntPtr dialogParent, bool useNativeUi) | ||
{ | ||
_inner ??= CreateInner(); | ||
return _inner.GetEnableHandle(dialogParent, useNativeUi); | ||
} | ||
|
||
public override MessageLoopHook CreateMessageLoopHook(IntPtr dialogParent = default, bool useNativeUi = false) | ||
{ | ||
_inner ??= CreateInner(); | ||
return _inner.CreateMessageLoopHook(dialogParent, useNativeUi); | ||
} | ||
|
||
public override IInvoker Invoker | ||
{ | ||
get | ||
{ | ||
_inner ??= CreateInner(); | ||
return _inner.Invoker; | ||
} | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
_inner?.Dispose(); | ||
_messagePump?.Dispose(); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters