public static void Remove()
        {
            var strings = new WeakRefCollection <string>();

            strings.Add("a");
            Assert.True(strings.Remove("a"));
            Assert.False(strings.Remove("a"));

            // comparer parameter is actually used
            strings.Add("b");
            Assert.True(strings.Remove("B", StringComparer.OrdinalIgnoreCase));
        }
Пример #2
0
        /// <summary>
        /// Enqueues <see cref="UnhandledExceptionEvent"/> for unhandled exceptions thrown on the specified <see cref="Dispatcher"/>.
        /// </summary>
        /// <param name="dispatcher">The dispatcher to catch unhandled exceptions from, or <c>null</c> for the UI dispatcher.</param>
        /// <param name="raiseEvents"><c>true</c> to make sure unhandled exceptions are enqueued; <c>false</c> to make sure they are not.</param>
        public static void RaiseUnhandledExceptionEventsFromDispatcher(Dispatcher dispatcher = null, bool raiseEvents = true)
        {
            if (dispatcher.NullReference())
            {
                dispatcher = Application.Current?.Dispatcher;
                if (dispatcher.NullReference())
                {
                    throw new InvalidOperationException("Could not determine UI dispatcher! (Application.Current is not available)");
                }
            }

            lock ( dispatchersRaisingUnhandledEvents )
            {
                if (raiseEvents)
                {
                    if (!dispatchersRaisingUnhandledEvents.Contains(dispatcher))
                    {
                        dispatchersRaisingUnhandledEvents.Add(dispatcher);
                        dispatcher.UnhandledException += OnDispatcherUnhandledException;
                    }
                }
                else
                {
                    if (dispatchersRaisingUnhandledEvents.Remove(dispatcher))
                    {
                        dispatcher.UnhandledException -= OnDispatcherUnhandledException;
                    }
                }
            }
        }