Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SerialPort"/> class, opening the serial port with the specified parameters.
        /// </summary>
        /// <param name="config">The serial port configuration parameters.</param>
        public SerialPort(Config config)
        {
            // Capture the synchronization context
            this.synchronizationContext = SynchronizationContext.Current;
            if (this.synchronizationContext == null)
            {
                this.synchronizationContext = new SynchronizationContext();
            }

            // Verify that the synchronization context is synchronized
            SynchronizationContextRegister.Verify(this.synchronizationContext.GetType(), SynchronizationContextProperties.Synchronized);

            // Copy configuration parameters into the wrapped serial port (saving the ones that must be set after it is opened)
            this.port = new System.IO.Ports.SerialPort(config.PortName, config.BaudRate, config.Parity, config.DataBits, config.StopBits);
            this.port.ParityReplace   = config.ParityReplace;
            this.port.ReadBufferSize  = config.ReadBufferSize;
            this.port.WriteBufferSize = config.WriteBufferSize;
            this.handshake            = config.Handshake;
            this.discardNull          = config.DiscardNull;

            // Hook up the wrapped serial port events
            this.port.DataReceived  += (_, e) => this.synchronizationContext.Post(__ => this.PortDataReceived(), null);
            this.port.PinChanged    += (_, e) => this.synchronizationContext.Post(__ => this.PortPinChanged(e.EventType), null);
            this.port.ErrorReceived += (_, e) => this.synchronizationContext.Post(__ => this.PortErrorReceived(e.EventType), null);
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GenericSynchronizingObject"/> class, binding to <see cref="SynchronizationContext.Current">SynchronizationContext.Current</see>.
        /// </summary>
        /// <example>
        /// The following code example demonstrates how GenericSynchronizingObject may be used to redirect FileSystemWatcher events to an ActionThread:
        /// <code source="..\..\Source\Examples\DocumentationExamples\GenericSynchronizingObject\WithFileSystemWatcher.cs"/>
        /// The code example above produces this output:
        /// <code lang="None" title="Output">
        /// ActionThread thread ID is 3
        /// FileSystemWriter.Created thread ID is 3
        /// </code>
        /// </example>
        public GenericSynchronizingObject()
        {
            // (This method is always invoked from a SynchronizationContext thread)
            this.synchronizationContext = SynchronizationContext.Current ?? new SynchronizationContext();

            if ((SynchronizationContextRegister.Lookup(this.synchronizationContext.GetType()) & SynchronizationContextProperties.SpecificAssociatedThread) == SynchronizationContextProperties.SpecificAssociatedThread)
            {
                this.synchronizationContextThreadId = Thread.CurrentThread.ManagedThreadId;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Timer"/> class, binding to <see cref="SynchronizationContext.Current">SynchronizationContext.Current</see>.
        /// </summary>
        /// <example>The following code sample demonstrates how to construct a single-shot Timer, start it, and handle the <see cref="Elapsed"/> event:
        /// <code source="..\..\Source\Examples\DocumentationExamples\Timer\SingleShot.cs"/>
        /// </example>
        public Timer()
        {
            // Capture the synchronization context
            this.synchronizationContext = SynchronizationContext.Current ?? new SynchronizationContext();

            // Verify that the synchronization context is synchronized
            SynchronizationContextRegister.Verify(this.synchronizationContext.GetType(), SynchronizationContextProperties.Synchronized);

            // Create the context for timer callbacks
            this.context = new CallbackContext();
        }
Esempio n. 4
0
        /// <summary>
        /// Synchronizes a delegate and then binds it to this context, and returns an asynchronous, synchronized, bound, valid delegate.
        /// </summary>
        /// <remarks>
        /// <para>The bound delegate will first determine if it is still valid. If the bound delegate is valid, then it will invoke the contained delegate. If the bound delegate is invalid, it will do nothing.</para>
        /// <para>To invalidate all bound delegates, call the <see cref="Reset"/> method.</para>
        /// </remarks>
        /// <param name="action">The contained delegate. This delegate should not raise exceptions.</param>
        /// <param name="synchronizationContext">The object to use for synchronizing the delegate if necessary.</param>
        /// <param name="checkSynchronizationContextVerification">Whether to verify that <paramref name="synchronizationContext"/> does support <see cref="SynchronizationContextProperties.Synchronized"/>.</param>
        /// <returns>A valid delegate bound to the current context.</returns>
        /// <threadsafety>
        /// <para>The returned delegate may be executed on any thread except the thread that owns <paramref name="synchronizationContext"/>; it will synchronize itself with this <see cref="CallbackContext"/>.</para>
        /// </threadsafety>
        public Action AsyncBind(Action action, SynchronizationContext synchronizationContext, bool checkSynchronizationContextVerification)
        {
            if (checkSynchronizationContextVerification)
            {
                // Verify that the synchronization context provides synchronization
                SynchronizationContextRegister.Verify(synchronizationContext.GetType(), SynchronizationContextProperties.Synchronized);
            }

            // Create the bound delegate
            Action boundAction = this.Bind(action);

            // Return a synchronized wrapper for the bound delegate
            return(() =>
            {
                synchronizationContext.Send((state) => boundAction(), null);
            });
        }
Esempio n. 5
0
        /// <summary>
        /// Synchronizes a delegate and then binds it to this context, and returns a synchronous, synchronized, bound, valid delegate.
        /// </summary>
        /// <remarks>
        /// <para>The bound delegate will first determine if it is still valid. If the bound delegate is valid, then it will invoke the contained delegate. If the bound delegate is invalid, it will do nothing.</para>
        /// <para>To invalidate all bound delegates, call the <see cref="Reset"/> method.</para>
        /// </remarks>
        /// <typeparam name="T">The return value of the contained and bound delegates.</typeparam>
        /// <param name="func">The contained delegate. This delegate should not raise exceptions.</param>
        /// <param name="synchronizationContext">The object to use for synchronizing the delegate.</param>
        /// <param name="checkSynchronizationContextVerification">Whether to verify that <paramref name="synchronizationContext"/> does support <see cref="SynchronizationContextProperties.Synchronized"/>.</param>
        /// <returns>A valid delegate bound to the current context.</returns>
        /// <threadsafety>
        /// <para>The returned delegate may be executed on any thread except the thread that owns <paramref name="synchronizationContext"/>; it will synchronize itself with this <see cref="CallbackContext"/>.</para>
        /// </threadsafety>
        public Func <T> Bind <T>(Func <T> func, SynchronizationContext synchronizationContext, bool checkSynchronizationContextVerification)
        {
            if (checkSynchronizationContextVerification)
            {
                // Verify that the synchronization context provides synchronization
                SynchronizationContextRegister.Verify(synchronizationContext.GetType(), SynchronizationContextProperties.Synchronized);
            }

            // Create the bound delegate
            Func <T> boundFunc = this.Bind(func);

            // Return a synchronized wrapper for the bound delegate
            return(() =>
            {
                T retVal = default(T);
                synchronizationContext.Send((state) => retVal = boundFunc(), null);
                return retVal;
            });
        }
Esempio n. 6
0
 /// <summary>
 /// Initializes static members of the <see cref="ActionDispatcherSynchronizationContext"/> class by registering with <see cref="SynchronizationContextRegister"/>.
 /// </summary>
 static ActionDispatcherSynchronizationContext()
 {
     SynchronizationContextRegister.Register(typeof(ActionDispatcherSynchronizationContext), SynchronizationContextProperties.Standard);
 }