コード例 #1
0
        /// <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 provideHandle, 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();
                Form form           = new Form();
                provideHandle(form.Handle);
                _WindowHandle  = form.Handle;
                _MessageFilter = new CustomMessageFilter(messageFilter);
                Application.AddMessageFilter(_MessageFilter);
                _Completion.Set();
                // If background = true we do proper resource cleaning on ProcessExit
                // event
                if (background)
                {
                    AppDomain.CurrentDomain.ProcessExit +=
                        new EventHandler(CurrentDomain_ProcessExit);
                }
                Application.Run(_ApplicationContext);
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = background;
            thread.Start();
            _Completion.WaitOne();
        }
コード例 #2
0
        /// <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();
                Form form           = new Form();
                initializeResources(form.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(form.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();
        }
コード例 #3
0
    public MessageLoop(HandleProvider provideHandle, MessageFilter messageFilter,
                       HandleProvider cleanResource)
    {
        _ResourceCleaner = cleanResource;
        _Completion      = new ManualResetEvent(false);
        _Disposed        = false;
        Thread thread = new Thread(delegate()
        {
            _ApplicationContext             = new ApplicationContext();
            _ApplicationContext.ThreadExit += new EventHandler(_ApplicationContext_ThreadExit);
            _Completion.Set();
            Form form = new Form();
            provideHandle(form.Handle);
            _WindowHandle  = form.Handle;
            _MessageFilter = new CustomMessageFilter(messageFilter);
            Application.AddMessageFilter(_MessageFilter);
            Application.Run(_ApplicationContext);
        });

        thread.SetApartmentState(ApartmentState.STA);
        thread.IsBackground = true;
        thread.Start();
    }