Exemple #1
0
 /// <summary>
 /// Adds an object of <see cref="ISynchronizedObject"/> to the <see cref="Synchronizer"/>.
 /// </summary>
 /// <param name="item">
 /// The object of <see cref="ISynchronizedObject"/> to add to the <see cref="Synchronizer"/>.
 /// </param>
 public void Add(ISynchronizedObject item)
 {
     if (synchronizedObjects != null)
     {
         synchronizedObjects.AddLast(item);
     }
 }
Exemple #2
0
        public ReaderLock(ISynchronizedObject synchronizedObject)
        {
            this.synchronizedObject = synchronizedObject;

            if (!Debugger.IsAttached)
                synchronizedObject.AcquireReaderLock();
        }
Exemple #3
0
        public ReaderLock(ISynchronizedObject synchronizedObject)
        {
            this.synchronizedObject = synchronizedObject;

            if (!Debugger.IsAttached)
            {
                synchronizedObject.AcquireReaderLock();
            }
        }
Exemple #4
0
        /// <summary>
        /// Removes the first occurrence of a specific object of <see cref="ISynchronizedObject"/>
        /// from the <see cref="Synchronizer"/>.
        /// </summary>
        /// <param name="item">The object to remove from the <see cref="Synchronizer"/>.</param>
        /// <returns>
        /// <c>true</c> if <paramref name="item"/> was successfully removed from the <see
        /// cref="Synchronizer"/>; otherwise, <c>false</c>. This method also returns <c>false</c> if
        /// <paramref name="item"/> is not found in the original <see cref="Synchronizer"/>.
        /// </returns>
        public bool Remove(ISynchronizedObject item)
        {
            if (synchronizedObjects != null)
            {
                return(synchronizedObjects.Remove(item));
            }

            return(false);
        }
Exemple #5
0
        /// <summary>
        /// Determines whether the <see cref="Synchronizer"/> contains a specific object of <see cref="ISynchronizedObject"/>.
        /// </summary>
        /// <param name="item">The object to locate in the <see cref="Synchronizer"/>.</param>
        /// <returns>
        /// <c>true</c> if <c>item</c> is found in the <see cref="Synchronizer"/>; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(ISynchronizedObject item)
        {
            if (synchronizedObjects != null)
            {
                return(synchronizedObjects.Contains(item));
            }

            return(false);
        }
        /// <summary>
        /// Raises an event safely, ensuring that all handlers are called on the proper thread, and any exceptions do not prevent other handlers being called.
        /// </summary>
        /// <param name="handler">The event to raise.</param>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">Event arguments for the event.</param>
        /// <exception cref="AggregateException">Thrown if any handlers raise exceptions, with the exceptions raised captured in the <see cref="AggregateException.InnerExceptions"/> property.</exception>
        /// <remarks>Temporarily made internal, as the use cases for this class really aren't clear enough to be confident of a suitable implementation of ISynchronizedObject.</remarks>
        public static void SafeRaise(this Delegate handler, ISynchronizedObject sender, EventArgs e)
        {
            sender.ThrowIfNull(nameof(sender));
            if (handler is null)
            {
                return;
            }

            if (handler.Method.GetParameters() is ParameterInfo[] handlerParams && (handlerParams.Length != 2 ||
                                                                                    !handlerParams[0].ParameterType.IsAssignableFrom(sender.GetType()) ||
                                                                                    !handlerParams[1].ParameterType.IsAssignableFrom(e?.GetType())))
            {
                throw new ArgumentException("Sender and event args must match handler parameter types.");
            }

            List <Exception> raisedExceptions = null;

            sender.SynchronizationContext.Post(
                state =>
            {
                foreach (Delegate del in handler.GetInvocationList())
                {
                    try
                    {
                        del.DynamicInvoke(sender, e);
                    }
                    catch (TargetInvocationException ex) when(ex.InnerException is Exception)
                    {
                        if (raisedExceptions is null)
                        {
                            raisedExceptions = new List <Exception>();
                        }

                        raisedExceptions.Add(ex.InnerException);
                    }
                }
            }, null);

            // Check list of exceptions is either still null, or not empty.
            Debug.Assert(raisedExceptions is null || raisedExceptions.Any(), "Empty list of exceptions after handling event.");
            if (raisedExceptions is List <Exception> )
            {
                throw new AggregateException(Properties.Resources.SafeRaiseExceptionMessage, raisedExceptions);
            }
        }
        /// <summary>
        /// Raises an event safely, ensuring that all handlers are called on the proper thread, and any exceptions do not prevent other handlers being called.
        /// </summary>
        /// <param name="handler">The event to raise.</param>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">Event arguments for the event.</param>
        /// <exception cref="AggregateException">Thrown if any handlers raise exceptions, with the exceptions raised captured in the <see cref="AggregateException.InnerExceptions"/> property.</exception>
        /// <remarks>Temporarily made internal, as the use cases for this class really aren't clear enough to be confident of a suitable implementation of ISynchronizedObject.</remarks>
        public static void SafeRaise(this EventHandler handler, ISynchronizedObject sender, EventArgs e)
        {
            sender.ThrowIfNull(nameof(sender));
            if (handler is null)
            {
                return;
            }

            List <Exception> raisedExceptions = null;

            sender.SynchronizationContext.Post(
                state =>
            {
                foreach (Delegate del in handler.GetInvocationList())
                {
                    try
                    {
                        del.DynamicInvoke(sender, e);
                    }
                    catch (TargetInvocationException ex) when(ex.InnerException is Exception)
                    {
                        if (raisedExceptions is null)
                        {
                            raisedExceptions = new List <Exception>();
                        }

                        raisedExceptions.Add(ex.InnerException);
                    }
                }
            }, null);

            // Check list of exceptions is either still null, or not empty.
            Debug.Assert(raisedExceptions is null || raisedExceptions.Any(), "Empty list of exceptions after handling event.");
            if (raisedExceptions is List <Exception> )
            {
                throw new AggregateException(Properties.Resources.SafeRaiseExceptionMessage, raisedExceptions);
            }
        }
Exemple #8
0
        public WriterLock(ISynchronizedObject synchronizedObject)
        {
            this.synchronizedObject = synchronizedObject;

            synchronizedObject.AcquireWriterLock();
        }
Exemple #9
0
        public WriterLock(ISynchronizedObject synchronizedObject)
        {
            this.synchronizedObject = synchronizedObject;

            synchronizedObject.AcquireWriterLock();
        }