public override void Send(SendOrPostCallback d, object state)
 {
     if (Application.UIThread != null && Application.UIThread.ManagedThreadId != Thread.CurrentThread.ManagedThreadId)
     {
         var       evt       = new ManualResetEventSlim(false);
         Exception exception = null;
         Application.Invoke(() => {
             try {
                 d.Invoke(state);
             } catch (Exception ex) {
                 exception = ex;
             } finally {
                 Thread.MemoryBarrier();
                 evt.Set();
             }
         }, toolkit);
         evt.Wait();
         if (exception != null)
         {
             throw exception;
         }
     }
     else
     {
         d.Invoke(state);
     }
 }
Ejemplo n.º 2
0
        public override void Send(SendOrPostCallback d, object state)
        {
#if NETFX_CORE
            d(state);
#else
            if (Thread.CurrentThread == mainThread)
            {
                d?.Invoke(state);
            }
            else
            {
                using (ManualResetEvent completed = new ManualResetEvent(false))
                {
                    Post(
                        _ =>
                    {
                        try
                        {
                            d?.Invoke(state);
                        }
                        finally
                        {
                            completed.Set();
                        }
                    },
                        null);

                    completed.WaitOne();
                }
            }
#endif
        }
 public override void Send(SendOrPostCallback d, object state)
 {
     if (_thread == Thread.CurrentThread)
         d.Invoke(state);
     else
         throw new NotSupportedException("Synchronously sending from different threads is not supported.");
 }
 public override void Send(SendOrPostCallback d, object state)
 {
     SynchronizationContext oldContext = SynchronizationContext.Current;
     SynchronizationContext.SetSynchronizationContext(this);
     d.Invoke(state);
     SynchronizationContext.SetSynchronizationContext(oldContext);
 }
Ejemplo n.º 5
0
            public void Send(SendOrPostCallback d, object state)
            {
                if (d == null)
                {
                    throw new ArgumentNullException("d");
                }

                if (!this)
                {
                    throw new ObjectDisposedException(GetType().Name);
                }

                if (_mainThreadContext == SynchronizationContext.Current)
                {
                    d.Invoke(state);
                }
                else
                {
                    using (var asyncResult = new InvokeResult(d, state))
                    {
                        lock (_actionQueue)
                        {
                            _actionQueue.Enqueue(asyncResult);
                        }

                        asyncResult.Wait();
                    }
                }
            }
 /// <summary>
 /// Dispatch a single job to the target thread and return immediately.
 /// This method can be called in background thread.
 /// </summary>
 /// <param name="callback">The callback which you wants to dispatch</param>
 public override void Post(SendOrPostCallback callback, object state)
 {
     jobs.Add(() =>
     {
         callback.Invoke(state);
     });
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Use SynchronizationContext.Send() if on the current SynchronizationContext
 /// Use Send if you need to get something done as soon as possible.
 /// </summary>
 /// <param name="d"></param>
 /// <param name="state"></param>
 public void Send(SendOrPostCallback d, object state = null)
 {
     try
     {
         if (_syncContext != SynchronizationContext.Current)
         {
             _syncContext.Send(d, state);
         }
         else
         {
             d?.Invoke(state);
         }
     }
     catch (InvalidAsynchronousStateException)
     {
         // Can happen on shutdown.
         // System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->
         // System.ComponentModel.InvalidAsynchronousStateException: An error occurred invoking the method.
         // The destination thread no longer exists.
     }
     catch (TargetInvocationException)
     {
         // Can happen on shutdown.
         // System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->
         // System.ComponentModel.InvalidAsynchronousStateException: An error occurred invoking the method.
         // The destination thread no longer exists.
     }
 }
Ejemplo n.º 8
0
 public override void Post(SendOrPostCallback d, object state)
 {
     if (loop != null && loop.IsRunning && loop.IsDrainingOrDrained == false)
     {
         loop.InvokeNextCycle(() => d.Invoke(state));
     }
 }
Ejemplo n.º 9
0
            public override void Post(SendOrPostCallback d, object state)
            {
                var reason = "Async";

                if (t == Time.CurrentTime)
                {
                    reason = t.CurrentReason;
                }

                if (reason.EndsWith(" (continued)") == false)
                {
                    reason += " (continued)";
                }

                t.QueueAction(reason, () =>
                {
                    var task = state as Task;
                    if (task != null && task.Status == TaskStatus.Faulted)
                    {
                        throw new AggregateException(task.Exception);
                    }
                    else
                    {
                        d.Invoke(state);

                        if (task != null && task.Status == TaskStatus.Faulted)
                        {
                            throw new PromiseWaitException(task.Exception);
                        }
                    }
                });
            }
Ejemplo n.º 10
0
 public override void Post(SendOrPostCallback d, object state)
 {
     PostUsed = true;
     d.Invoke(state);                        //This is not the correct symantics for Post,
     //but it is called here so that we can prove
     //the callback is correct.
 }
Ejemplo n.º 11
0
    public virtual void Send(SendOrPostCallback d, object state)
    {
        Exception threadException = null;

        try
        {
            innerContext.Send(_ =>
            {
                try
                {
                    d.Invoke(state);
                }
                catch (Exception exception)
                {
                    threadException = exception;
                }
            }, null);
        }
        catch (Exception ex)
        {
        }
        if (threadException != null)
        {
            throw new Exception("Synchronization error", threadException);
        }
    }
Ejemplo n.º 12
0
        public override void Send(SendOrPostCallback d, object state)
        {
            SynchronizationContext oldContext = SynchronizationContext.Current;

            SynchronizationContext.SetSynchronizationContext(this);
            d.Invoke(state);
            SynchronizationContext.SetSynchronizationContext(oldContext);
        }
Ejemplo n.º 13
0
 public override void Post(SendOrPostCallback d, object state)
 {
     synchronizationImplementation.Post(passedState =>
     {
         SetSynchronizationContext(this);
         cultureHolder.ApplyCulture();
         d.Invoke(s);
     }, state);
 }
                public override void Post(SendOrPostCallback callback, object state)
                {
                    Server.CallFuncEvent e = new Server.CallFuncEvent(() =>
                    {
                        callback.Invoke(state);
                    });

                    server.Enqueue(e);
                }
Ejemplo n.º 15
0
            public void Invoke()
            {
                SynchronizationEventData tmp = this;

                EventLoop.OnEventExecuting(Id, ref tmp);
                Callback?.Invoke(State);
                EventLoop.OnEventExecuted(Id, tmp, EventStatus.Continue);
                WaitHandle?.Set();
            }
 public override void Post(SendOrPostCallback d, object state)
 {
     synchronizationImplementation.Post(passedState =>
             {
                 SetSynchronizationContext(this);
                 cultureHolder.ApplyCulture();
                 d.Invoke(passedState);
             }, state);
 }
 public override void Post(SendOrPostCallback d, object state)
 {
     ThreadPool.QueueUserWorkItem(_ =>
     {
         SynchronizationContext oldContext = SynchronizationContext.Current;
         SynchronizationContext.SetSynchronizationContext(this);
         d.Invoke(state);
         SynchronizationContext.SetSynchronizationContext(oldContext);
     }, null);
 }
Ejemplo n.º 18
0
 public override void Post(SendOrPostCallback d, object state)
 {
     ThreadPool.QueueUserWorkItem(_ =>
     {
         SynchronizationContext oldContext = SynchronizationContext.Current;
         SynchronizationContext.SetSynchronizationContext(this);
         d.Invoke(state);
         SynchronizationContext.SetSynchronizationContext(oldContext);
     }, null);
 }
Ejemplo n.º 19
0
        public override void Post(SendOrPostCallback d, object state)
        {
            Console.WriteLine("POST");

            var job = new Thread(() => d?.Invoke(state));

            job.Name = "Custom SC thread";

            job.Start();
        }
Ejemplo n.º 20
0
        public override void Post(SendOrPostCallback d, object state)
        {
            Thread thread = new Thread(_ => d.Invoke(state))
            {
                Name         = "PostThread",
                IsBackground = true
            };

            thread.Start();
        }
 public override void Send(SendOrPostCallback d, object state)
 {
     if (_thread == Thread.CurrentThread)
     {
         d.Invoke(state);
     }
     else
     {
         throw new NotSupportedException("Synchronously sending from different threads is not supported.");
     }
 }
Ejemplo n.º 22
0
        public override void Post(SendOrPostCallback action, object state)
        {
            SendOrPostCallback actionWrap = state2 =>
            {
                SetSynchronizationContext(new ConsoleSyncContext());
                action.Invoke(state2);
            };
            var callback = new WaitCallback(actionWrap.Invoke);

            ThreadPool.QueueUserWorkItem(callback, state);
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Use SynchronizationContext.Post() if on the current SynchronizationContext
 /// Use Post to wait our turn in the queue.
 /// </summary>
 /// <param name="d"></param>
 /// <param name="state"></param>
 public void Post(SendOrPostCallback d, object state = null)
 {
     if (_syncContext != SynchronizationContext.Current)
     {
         _syncContext.Post(d, state);
     }
     else
     {
         d?.Invoke(state);
     }
 }
Ejemplo n.º 24
0
 public void Invoke(UnityEngine.Object context)
 {
     try
     {
         _callback.Invoke(_userState);
     }
     catch (Exception e)
     {
         Debug.LogException(e, context);
     }
 }
Ejemplo n.º 25
0
 public override void Post(SendOrPostCallback d, object state)
 {
     try
     {
         d.Invoke(state);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Ejemplo n.º 26
0
        public override void Post(SendOrPostCallback d, object state)
        {
            if (Thread.CurrentThread.Name is null)
            {
                Thread.CurrentThread.Name = "NAME_THREAD";
            }

            Program.PrintInfo("Pool", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.Name, Thread.CurrentThread.IsThreadPoolThread);

            d.Invoke(state);
        }
Ejemplo n.º 27
0
 public override void Send(SendOrPostCallback d, object state)
 {
     if (Thread.CurrentThread != loop?.Thread && loop != null && loop.IsRunning && loop.IsDrainingOrDrained == false)
     {
         loop.InvokeNextCycle(() => d.Invoke(state));
     }
     else
     {
         throw new NotSupportedException();
     }
 }
Ejemplo n.º 28
0
 public override void Post(SendOrPostCallback d, object state)
 {
     actions.Enqueue(() => {
         try {
             d.Invoke(state);
         }
         catch (Exception e) {
             Logging.tML.Error("Posted event", e);
         }
     });
 }
Ejemplo n.º 29
0
 public override void Post(SendOrPostCallback d, object state)
 {
     try
     {
         throw new Exception("Exception in ConsoleSenhronizationContext");
         d.Invoke(state);
     }
     catch (Exception e)
     {
         Console.WriteLine($"Error!!!!!!!! {e.Message}");
     }
 }
Ejemplo n.º 30
0
        /// <summary>
        /// Dispatches a synchronous message to a synchronization context.
        /// </summary>
        /// <param name="d">The <see cref="T:System.Threading.SendOrPostCallback" /> delegate to call.</param>
        /// <param name="state">The object passed to the delegate.</param>
        /// <exception cref="System.ArgumentNullException">d</exception>
        public override void Send(SendOrPostCallback d, object state)
        {
            if (d == null)
            {
                throw new ArgumentNullException("d");
            }

            Application.Current.InvokeOnMain(() =>
            {
                d.Invoke(state);
            });
        }
        /// <summary>
        /// Helper method to raise an event.
        /// </summary>
        /// <example>
        /// Call it like this
        /// <code>
        ///     TelemetryEventArgs args = new TelemetryEventArgs(
        ///         new acTelemetryInfo(telemetryData, telemetryData));
        ///     RaiseEvent(OnTelemetryUpdated, args);
        /// </code>
        /// </example>
        protected void RaiseEvent <T>(Action <T> del, T e) where T : EventArgs
        {
            var callback = new SendOrPostCallback(obj => del(obj as T));

            if (context != null)
            {
                context.Post(callback, e);
            }
            else
            {
                callback.Invoke(e);
            }
        }
Ejemplo n.º 32
0
		public override void Send (SendOrPostCallback d, object state)
		{
			if (Application.UIThread != null && Application.UIThread.ManagedThreadId != Thread.CurrentThread.ManagedThreadId) {
				var evt = new ManualResetEventSlim (false);
				Exception exception = null;
				Application.Invoke (() => {
					try {
						d.Invoke (state);
					} catch (Exception ex) {
						exception = ex;
					} finally {
						Thread.MemoryBarrier ();
						evt.Set ();
					}
				});
				evt.Wait ();
				if (exception != null)
					throw exception;
			} else {
				d.Invoke (state);
			}
		}
Ejemplo n.º 33
0
            public override void Post(SendOrPostCallback d, object?state)
            {
                if (testPostedAction is null)
                {
                    Assert.Fail("Expected no calls to SynchronizationContext.Post.");
                }
                if (ReceivedPost)
                {
                    Assert.Fail("Expected no more than one call to SynchronizationContext.Post.");
                }
                ReceivedPost = true;

                testPostedAction !.Invoke(() => d.Invoke(state));
            }
            public override void Send(SendOrPostCallback d, object state)
            {
                var original = Current;

                try
                {
                    SetSynchronizationContext(this);
                    d.Invoke(state);
                }
                finally
                {
                    SetSynchronizationContext(original);
                }
            }
Ejemplo n.º 35
0
        /// <summary>
        /// Dispatches a message to a synchronization context.
        /// </summary>
        /// <param name="context">The target context.</param>
        /// <param name="d">The delegate to invoke.</param>
        /// <param name="state">User-defined state.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="d"/> is <see langword="null"/>.</exception>
        public static void Invoke(this SynchronizationContext context, SendOrPostCallback d, object state)
        {
            if (context == SynchronizationContext.Current)
            {
                if (d == null)
                {
                    throw new ArgumentNullException(nameof(d));
                }

                d.Invoke(state);
            }
            else
            {
                context.Post(d, state);
            }
        }
Ejemplo n.º 36
0
        private void RaiseEvent <T>(Action <T> del, T e)
            where T : EventArgs
        {
            var callback = new SendOrPostCallback(obj => del(obj as T));

            if (context != null && this.EventRaiseType == EventRaiseTypes.CurrentThread)
            {
                // Post the event method on the thread context, this raises the event on the thread on which the SdkWrapper object was created
                context.Post(callback, e);
            }
            else
            {
                // Simply invoke the method, this raises the event on the background thread that the SdkWrapper created
                // Care must be taken by the user to avoid cross-thread operations
                callback.Invoke(e);
            }
        }
Ejemplo n.º 37
0
        /// <summary>
        /// May be called from any thread.
        /// </summary>
        public override void Send(SendOrPostCallback d, object state)
        {
            Guard.ArgumentNotNull(d, nameof(d));

            if (SynchronizationContext.Current == this)
            {
                d.Invoke(state);
            }
            else
            {
                using (var finished = new ManualResetEventSlim())
                {
                    AddWork(new ScheduledWork(d, state, finished));
                    finished.Wait();
                }
            }
        }
Ejemplo n.º 38
0
		public override void Send (SendOrPostCallback d, object state)
		{
			Exception ex = null;
			using (var waiter = new ManualResetEventSlim (false)) {
				Post (wrapper => {
					try {
						d.Invoke (state);
					} catch (Exception e) {
						ex = e;
					} finally {
						waiter.Set ();
					}
				}, null);
				waiter.Wait ();
			}

			if (ex != null)
				throw ex;
		}
 public override void Post(SendOrPostCallback d, object state)
 {
     _dispatcher.BeginInvoke(() => d.Invoke(state));
 }
Ejemplo n.º 40
0
		public override void Send (SendOrPostCallback d, object state)
		{
			var evt = new ManualResetEventSlim (false);
			Exception exception = null;
			Post (s => {
				try {
					d.Invoke (state);
				} catch (Exception ex) {
					exception = ex;
				} finally {
					Thread.MemoryBarrier ();
					evt.Set ();
				}
			}, null);
			evt.Wait ();
			if (exception != null)
				throw exception;
		}
Ejemplo n.º 41
0
		public override void Post (SendOrPostCallback d, object state)
		{
			Application.Invoke (() => d.Invoke (state));
		}
 public override void Post(SendOrPostCallback d, object state)
 {
     d.Invoke(state);
 }
        /// <summary>
        /// Dispatch a single job to the target thread and wait until the job ends.
        /// This method can be called in background thread.
        /// </summary>
        /// <param name="callback">The callback which you wants to dispatch</param>
        public override void Send(SendOrPostCallback callback, object state)
        {
            AutoResetEvent ev = new AutoResetEvent(false);
            jobs.Add(() =>
            {
                callback.Invoke(state);

                ev.Set();
            });

            ev.WaitOne();
        }
Ejemplo n.º 44
0
		private void RunInSafeContext(SendOrPostCallback core)
		{
			if (UiSynchronizationContext != null)
			{
				UiSynchronizationContext.Send(core, null);
			}
			else
			{
				core.Invoke(null);
			}
		}