4

C#で非同期的に低レベルのメッセージを処理/フィルタリングするための2番目のメッセージループを作成しようとしています。これは、非表示のフォームを作成し、そのHandleプロパティを公開してフックし、別のスレッドで2番目のメッセージループを実行することで機能します。現時点では結果には非常に満足していますが、2番目のループを適切に終了することができません。唯一の回避策は、IsBackgroundプロパティをtrueに設定することでした。そのため、2番目のスレッドは、メインアプリケーションの終了時に(保留中のメッセージをすべて処理せずに)単純に終了します。

問題は、2番目のApplication.Run()が戻るように、そのメッセージループを適切に終了する方法です。別のApplicationContextを作成し、さまざまなイベント(Application.ApplicationExit、Application.ThreadExit、ApplicationContext.ThreadExit)を制御するさまざまなアプローチを試しましたが、デバッグできない競合状態ですべて失敗しました。

ヒントはありますか?ありがとう

これはコードです:

public class MessagePump
{
    public delegate void HandleHelper(IntPtr handle);

    public MessagePump(HandleHelper handleHelper, Filter filter)
    {
        Thread thread = new Thread(delegate()
        {
            ApplicationContext applicationContext = new ApplicationContext();
            Form form = new Form();
            handleHelper(form.Handle);
            Application.AddMessageFilter(new MessageFilter(filter));
            Application.Run(applicationContext);
        });
        thread.SetApartmentState(ApartmentState.STA);
        thread.IsBackground = true; // <-- The workaround
        thread.Start();
    }
}

public delegate bool Filter(ref Message m);

internal class MessageFilter : IMessageFilter
{
    private Filter _Filter;

    public MessageFilter(Filter filter)
    {
        _Filter = filter;
    }

    #region IMessageFilter Members

    public bool PreFilterMessage(ref Message m)
    {
        return _Filter(ref m);
    }

    #endregion // IMessageFilter Members
}

メインのFormコンストラクターで次のように使用します。

_Completion = new ManualResetEvent(false);

MessagePump pump = new MessagePump(
delegate(IntPtr handle)
{
    // 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)
{
    // Asyncrhronous processing of the messages
    // When the correct message is found -->
    _Completion.Set();
}

編集:私の答えの完全な解決策。

4

2 に答える 2

3

FormインスタンスをApplicationContextパラメーターとして ctorに渡す必要があります。

applicationContext = new ApplicationContext(form); 

現在、基本的に、フォームが閉じられていることを気にしないコンテキストのないインスタンスを作成しています。

また、不要になったフィルターを削除するなど、クリーンアップを行うことをお勧めします。

Form form = new Form();
ApplicationContext applicationContext = new ApplicationContext(form);
handleHelper(form.Handle);

MessageFilter filter = new MessageFilter(filter);
Application.AddMessageFilter(filter);
Application.Run(applicationContext);
Application.RemoveMessageFilter(filter);

[編集]

フォームを表示したくない場合は、パラメーターなしの ctor を使用できますが、ApplicationContext.ExitThreadメソッドを呼び出してコンテキストを手動で閉じる必要があります。FormClosedこのメソッドは、コンストラクターでフォームを渡すと、フォームがイベントを発生させたときに実際に呼び出されます。

非表示のフォームはコンテキストとは関係がないため、いずれも終了する必要があります。

于 2011-01-27T11:45:25.080 に答える
3

thread.IsBackground = true;「ねえ、私は最後に実行されているスレッドであり、終了することになっている」と判断する唯一の方法だったので、最終的にはそれが悪いことではないことに気付きました。適切なリソースのクリーニングは依然として必要であり、困難です。このためには、リソース クリーニング用の 3 番目のデリゲートが必要です。これを 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();
于 2011-01-27T15:45:26.167 に答える