Changeset 2 for swappable message loop hook implementations.

This commit is contained in:
soukoku
2014-05-25 08:45:52 -04:00
parent 61b0e89313
commit 764c75a7a0
12 changed files with 285 additions and 63 deletions

View File

@@ -14,7 +14,7 @@ namespace NTwain.Internals
/// <returns></returns>
TWIdentity AppId { get; }
MessageLoop SelfMessageLoop { get; }
MessageLoopHook MessageLoopHook { get; }
/// <summary>
/// Gets or sets a value indicating whether calls to triplets will verify the current twain session state.

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NTwain.Internals
{
/// <summary>
/// Interface for checking whether messages from WndProc is a TWAIN message and is handled
/// internally.
/// </summary>
interface IWinMessageFilter
{
/// <summary>
/// Checks and handle the message if it's a TWAIN message.
/// </summary>
/// <param name="hwnd">The window handle.</param>
/// <param name="msg">The message.</param>
/// <param name="wParam">The w parameter.</param>
/// <param name="lParam">The l parameter.</param>
/// <returns>true if handled internally.</returns>
bool IsTwainMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
}
}

View File

@@ -1,28 +1,27 @@
using NTwain.Properties;
using NTwain.Triplets;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Threading;
namespace NTwain.Internals
{
/// <summary>
/// Provides a message loop for old TWAIN to post or new TWAIN to synchronize callbacks.
/// </summary>
class MessageLoop
sealed class InternalMessageLoopHook : MessageLoopHook
{
Dispatcher _dispatcher;
WindowsHook _hook;
public void Stop()
internal override void Stop()
{
if (_dispatcher != null)
{
_dispatcher.InvokeShutdown();
}
}
public void Start(IWinMessageFilter filter)
internal override void Start(IWinMessageFilter filter)
{
if (_dispatcher == null)
{
@@ -36,6 +35,7 @@ namespace NTwain.Internals
if (!Platform.IsOnMono)
{
_hook = new WindowsHook(filter);
Handle = _hook.Handle;
}
hack.Set();
Dispatcher.Run();
@@ -55,22 +55,14 @@ namespace NTwain.Internals
}
}
public IntPtr LoopHandle
{
get
{
return _hook == null ? IntPtr.Zero : _hook.Handle;
}
}
public void BeginInvoke(Action action)
internal override void BeginInvoke(Action action)
{
if (_dispatcher == null) { throw new InvalidOperationException(Resources.MsgLoopUnavailble); }
_dispatcher.BeginInvoke(DispatcherPriority.Normal, action);
}
public void Invoke(Action action)
internal override void Invoke(Action action)
{
if (_dispatcher == null) { throw new InvalidOperationException(Resources.MsgLoopUnavailble); }