///////////////////////////////////////////////////////////////////////////////////////////////// // // StartEventHandler() // // Create the background thread on which the UIA event handler must run. // // This function runs on the main UI thread. // ///////////////////////////////////////////////////////////////////////////////////////////////// public void StartEventHandler() { // This sample doesn't expect to enter here with a background thread already initialized. if (_threadBackground != null) { return; } // The main thread will notify the background thread later when it's time to close down. _autoEventMsg = new AutoResetEvent(false); // Create the background thread, and wait until it's ready to start working. _autoEventInit = new AutoResetEvent(false); ParameterizedThreadStart paramThreadStart = new ParameterizedThreadStart(s_ThreadProc); _threadBackground = new Thread(paramThreadStart); _threadBackground.SetApartmentState(ApartmentState.MTA); // The event handler must run on an MTA thread. _threadBackground.Start(this); _autoEventInit.WaitOne(); // The background thread is up and running. Have it add the UIA event handler now. SampleEventMsgData msgData = new SampleEventMsgData(); msgData.msgType = SampleMsgType.msgRegisterEventHandler; AddMsgToQueue(msgData); }
///////////////////////////////////////////////////////////////////////////////////////////////// // // Uninitialize() // // Called on main UI thread to close the background thread down. // ///////////////////////////////////////////////////////////////////////////////////////////////// public void Uninitialize() { // Tell the background thread to close down. if (_threadBackground != null) { SampleEventMsgData msgData = new SampleEventMsgData(); msgData.msgType = SampleMsgType.msgCloseDown; AddMsgToQueue(msgData); } }
///////////////////////////////////////////////////////////////////////////////////////////////// // // Uninitialize() // // Called on main UI thread to close the background thread down. // ///////////////////////////////////////////////////////////////////////////////////////////////// public void Uninitialize() { // Tell the background thread to close down. if (_threadBackground != null) { SampleEventMsgData msgData = new SampleEventMsgData(); msgData.msgType = SampleMsgType.msgCloseDown; AddMsgToQueue(msgData); } // Release all the objects created on startup now. CleanUp(); }
///////////////////////////////////////////////////////////////////////////////////////////////// // // AddMsgToQueue() // // Update a list of messages waiting to be processed by the background thread. // // Run on the main UI thread. // ///////////////////////////////////////////////////////////////////////////////////////////////// private void AddMsgToQueue(SampleEventMsgData msgData) { // Request the lock, and block until it is obtained. Monitor.Enter(_msgQueue); try { // When the lock is obtained, add an element. _msgQueue.Enqueue(msgData); } finally { // Ensure that the lock is released. Monitor.Exit(_msgQueue); } _autoEventMsg.Set(); }