我最终意识到这thread.IsBackground = true;
还不错,因为这是确定“嘿,我是最后一个运行的线程,我应该退出”的唯一方法。仍然需要正确的资源清理,很难。为此,需要第三个资源清理委托,我刚刚将其注册到 AppDomain.CurrentDomain.ProcessExit 事件。我什至为 MessageLoop 类提供了一个 ExitLoop() 方法(问题中是 MessagePump)。这样,我可以随时终止消息循环。ExitLoop() 和 ProcessExit 处理程序的关键部分是互斥的。
编码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace System
{
public class MessageLoop
{
#region Fields
private Object _Lock;
private ApplicationContext _ApplicationContext;
private CustomMessageFilter _MessageFilter;
private HandleProvider _ResourceCleaner;
private ManualResetEvent _Completion;
private bool _Disposed;
#endregion // Fields
#region Constructors
/// <summary>
/// Run a second message pump that will filter messages asyncronously
/// </summary>
/// <param name="provideHandle">A delegate that provide a window handle for
/// resource initializing</param>
/// <param name="messageFilter">A delegate for message filtering</param>
/// <param name="cleanResources">A delegate for proper resource cleaning
/// before quitting the loop</param>
/// <param name="background">State if the loop should be run on a background
/// thread or not. If background = false, please be aware of the
/// possible race conditions on application shut-down.</param>
public MessageLoop(HandleProvider initializeResources, MessageFilter messageFilter,
HandleProvider cleanResources, bool background)
{
_Lock = new Object();
_ResourceCleaner = cleanResources;
_Completion = new ManualResetEvent(false);
_Disposed = false;
Thread thread = new Thread(delegate()
{
_ApplicationContext = new ApplicationContext();
WindowHandle window = new WindowHandle();
initializeResources(window.Handle);
_MessageFilter = new CustomMessageFilter(messageFilter);
Application.AddMessageFilter(_MessageFilter);
// Signal resources initalizated
_Completion.Set();
// If background = true, do resource cleaning on ProcessExit event
if (background)
{
AppDomain.CurrentDomain.ProcessExit +=
new EventHandler(CurrentDomain_ProcessExit);
}
// Run the message loop
Application.Run(_ApplicationContext);
// Clean resource before leaving the thread
cleanResources(window.Handle);
// Signal resources cleaned
_Completion.Set();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = background;
thread.Start();
// Before returning the instace, wait for thread resources initialization
_Completion.WaitOne();
}
#endregion // Constructors
#region Inquiry
/// <summary>
/// Early exit the message loop
/// </summary>
public void ExitLoop()
{
lock (_Lock)
{
if (_Disposed)
return;
// Completion was already signaled in the constructor
_Completion.Reset();
// Tell the message loop thread to quit
_ApplicationContext.ExitThread();
// Wait for thread resources cleaning
_Completion.WaitOne();
_Disposed = true;
}
}
#endregion // Inquiry
#region Event handlers
void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
lock (_Lock)
{
if (_Disposed)
return;
// Completion was already signaled in the constructor
_Completion.Reset();
// Tell the message loop thread to quit
_ApplicationContext.ExitThread();
// Wait for thread resources cleaning
_Completion.WaitOne();
_Disposed = true;
}
}
#endregion // Event handlers
#region Support
public delegate void HandleProvider(IntPtr handle);
public delegate bool MessageFilter(ref Message m);
internal class CustomMessageFilter : IMessageFilter
{
private MessageFilter _Filter;
public CustomMessageFilter(MessageFilter filter)
{
_Filter = filter;
}
#region IMessageFilter Members
public bool PreFilterMessage(ref Message m)
{
return _Filter(ref m);
}
#endregion // IMessageFilter Members
}
#endregion // Support
}
public class WindowHandle : NativeWindow
{
public WindowHandle()
{
CreateParams parms = new CreateParams();
CreateHandle(parms);
}
~WindowHandle()
{
DestroyHandle();
}
}
}
可以这样使用:
_Completion = new ManualResetEvent(false);
MessageLoop messageLoop = new MessageLoop(
delegate(IntPtr handle) // Resource initializing
{
// Sample code, I did this form twain drivers low level wrapping
_Scanner = new TwainSM(handle);
_Scanner.LoadDs("EPSON Perfection V30/V300");
},
delegate(ref Message m) // Message filtering
{
// Asyncrhronous processing of the messages
// When the correct message is found -->
_Completion.Set();
},
delegate(IntPtr handle) // Resource cleaning
{
// Resource cleaning/disposing. In my case, it's the following...
_Scanner.Dispose();
}, true); // Automatically quit on main application shut-down
// Anytime you can exit the loop
messageLoop.ExitLoop();