void Win7UIAClientForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_synth != null)
            {
                _synth.SpeakAsyncCancelAll();
                _synth = null;
            }

            if (_highlight != null)
            {
                _highlight.Uninitialize();
                _highlight = null;
            }

            if (_sampleFocusEventHandler != null)
            {
                _sampleFocusEventHandler.Uninitialize();
                _sampleFocusEventHandler = null;
            }

            // Give any background threads created on startup a chance to close down gracefully.
            Thread.Sleep(200);
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // Win7UIAClientForm_FormClosed()
        //
        // Uninitialize and release everything created on startup.
        //
        /////////////////////////////////////////////////////////////////////////////////////////////////
        void Win7UIAClientForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_linkProcessor != null)
            {
                _linkProcessor.Uninitialize();
                _linkProcessor = null;
            }

            if (_highlight != null)
            {
                _highlight.Uninitialize();
                _highlight = null;
            }

            if (_sampleEventHandler != null)
            {
                _sampleEventHandler.Uninitialize();
                _sampleEventHandler = null;
            }

            // Give any background threads created on startup a chance to close down gracefully.
            Thread.Sleep(200);
        }
        void InitializeSample()
        {
            // Initialize the highlighting used to magnify the element with focus.
            _highlight = new Highlight();
            _highlight.Initialize();

            try
            {
                _synth = new SpeechSynthesizer();
            }
            catch
            {
                // Allow this sample to run even if speech is not available.
                _synth = null;
            }

            // Initialize the event handler which is called when focus changes.
            SampleUIEventHandlerDelegate sampleUIEventHandlerDelegate = new SampleUIEventHandlerDelegate(HandleEventOnUIThread);

            _sampleFocusEventHandler = new SampleFocusEventHandler(this, sampleUIEventHandlerDelegate);

            // Now tell the sample event handler to register for events from UIA.
            _sampleFocusEventHandler.StartEventHandler();
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // Initialize()
        //
        // In order to prevent any possibility of the calls to UIA below leading to delays while a
        // a UIA event handler is also running, all calls to UIA below will run on a background MTA
        // thread. This means that the app's main UI thread makes no calls to UIA at all.
        //
        // Ths method runs on the main UI thread.
        //
        /////////////////////////////////////////////////////////////////////////////////////////////////
        public void Initialize(Win7UIAClientForm_LinkProcessor.SampleUIListUpdateDelegate UIUpdateDelegate, ListView listViewLinks, Highlight highlight)
        {
            _UIUpdateDelegate = UIUpdateDelegate;
            _listViewLinks    = listViewLinks;
            _highlight        = highlight;

            // The object will call UIA on a background MTA thread. This sample doesn't
            // expect to enter here with a background thread already initialized.
            if (_threadBackground != null)
            {
                return;
            }

            // The background thread will wait for notifications when action is required.
            _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_DoWork);

            _threadBackground = new Thread(paramThreadStart);
            _threadBackground.SetApartmentState(ApartmentState.MTA);
            _threadBackground.Start(this);

            _autoEventInit.WaitOne();
        }