Example #1
0
 // Callback method called by the Win32 multimedia timer when a timer
 // periodic event occurs.
 private void TimerPeriodicEventCallback(int id, int msg, int user, int param1, int param2)
 {
     if (synchronizingObject != null)
     {
         synchronizingObject.BeginInvoke(tickRaiser, new object[] { EventArgs.Empty });
     }
     else
     {
         OnTick(EventArgs.Empty);
     }
 }
Example #2
0
 protected override void OnListChanged(ListChangedEventArgs e)
 {
     if (syncInvoke == null)
     {
         base.OnListChanged(e); return;
     }
     if (syncInvoke.InvokeRequired)
     {
         syncInvoke.BeginInvoke(new BaseOnListChangedDelegate(StaticBaseOnListChanged), new object[] { this, e });
     }
     else
     {
         base.OnListChanged(e);
     }
 }
Example #3
0
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler;

            lock (SyncRoot)
                handler = PropertyChanged;

            if (handler != null)
            {
                ISynchronizeInvoke invoker = Invoker;
                if (invoker != null && invoker.InvokeRequired)
                {
                    invoker.BeginInvoke(handler, new object[] { this, new PropertyChangedEventArgs(propertyName) });
                }
                else
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }

                if (propertyName != "CheckedStatus" && propertyName != "State")
                {
                    CheckedStatus = CheckedStatus.Unknown;
                    lock (_actions.SyncRoot)
                        foreach (ActionBase action in _actions)
                        {
                            action.CheckedStatus = CheckedStatus.Unknown;
                        }
                }
            }
        }
Example #4
0
 // Utility function for firing an event through the target.
 // It uses C#'s variable length parameter list support
 // to build the parameter list.
 // This functions presumes that the caller holds the object lock.
 // (This is because the event list is typically modified on the UI
 // thread, but events are usually raised on the worker thread.)
 protected void FireAsync(Delegate dlg, params object[] pList)
 {
     if (dlg != null)
     {
         isiTarget.BeginInvoke(dlg, pList);
     }
 }
Example #5
0
 // Extension method which marshals events back onto the main thread
 public static void RaiseMarshalled(this MulticastDelegate multicast, object sender, EventArgs args)
 {
     foreach (Delegate del in multicast.GetInvocationList())
     {
         // Try for WPF first
         DispatcherObject dispatcherTarget = del.Target as DispatcherObject;
         if (dispatcherTarget != null && !dispatcherTarget.Dispatcher.CheckAccess())
         {
             // WPF target which requires marshaling
             dispatcherTarget.Dispatcher.BeginInvoke(del, sender, args);
         }
         else
         {
             // Maybe its WinForms?
             ISynchronizeInvoke syncTarget = del.Target as ISynchronizeInvoke;
             if (syncTarget != null && syncTarget.InvokeRequired)
             {
                 // WinForms target which requires marshaling
                 syncTarget.BeginInvoke(del, new object[] { sender, args });
             }
             else
             {
                 // Just do it.
                 del.DynamicInvoke(sender, args);
             }
         }
     }
 }
Example #6
0
        // Extension method which marshals actions back onto the main thread
        public static void RaiseMarshalled <T>(this Action <T> action, T args)
        {
            // Try for WPF first
            DispatcherObject dispatcherTarget = action.Target as DispatcherObject;

            if (dispatcherTarget != null && !dispatcherTarget.Dispatcher.CheckAccess())
            {
                // WPF target which requires marshaling
                dispatcherTarget.Dispatcher.BeginInvoke(action, args);
            }
            else
            {
                // Maybe its WinForms?
                ISynchronizeInvoke syncTarget = action.Target as ISynchronizeInvoke;
                if (syncTarget != null && syncTarget.InvokeRequired)
                {
                    // WinForms target which requires marshaling
                    syncTarget.BeginInvoke(action, new object[] { args });
                }
                else
                {
                    // Just do it.
                    action.DynamicInvoke(args);
                }
            }
        }
Example #7
0
        public static void RaiseEventOnUIThread(Delegate theEvent, ClientData arg1, Dictionary <string, byte[]> arg2)
        {
            if (theEvent == null)
            {
                return;
            }

            foreach (Delegate d in theEvent.GetInvocationList())
            {
                ISynchronizeInvoke syncer = d.Target as ISynchronizeInvoke;
                if (syncer == null)
                {
                    try
                    {
                        d.DynamicInvoke(new object[] { arg1, arg2 });
                    }
                    catch { }
                }
                else
                {
                    try
                    {
                        syncer.BeginInvoke(d, new object[] { arg1, arg2 });
                    }
                    catch { }
                }
            }
        }
Example #8
0
        private void InvokeDelegate(Delegate del, object[] args)
        {
            ISynchronizeInvoke target = del.Target as ISynchronizeInvoke;

            if (target != null)
            {
                if (!target.InvokeRequired)
                {
                    del.DynamicInvoke(args);
                }
                else
                {
                    try
                    {
                        target.BeginInvoke(del, args);
                    }
                    catch
                    {
                    }
                }
            }
            else
            {
                del.DynamicInvoke(args);
            }
        }
Example #9
0
    protected void OnConnectEvent(EventArgs e)
    {
        ConnectHandler ev = ConnectEvent;

        //This may not be the exact right syntax.
        _syncSource.BeginInvoke(ev, this, e);
    }
Example #10
0
        public static void InvokeLog(this ISynchronizeInvoke si, Delegate method, object[] args)
        {
            int pid = Thread.CurrentThread.ManagedThreadId;

            List <string> IST = null;
            bool          listExists;

            lock (dicoLock)
                listExists = InvokeStackTraces.TryGetValue(32, out IST);

            if (listExists)
            {
                IST = new List <string>();

                lock (dicoLock)
                    InvokeStackTraces.Add(pid, IST);
            }
            IST.Add(Environment.StackTrace);

            var iar = si.BeginInvoke(method, args);

            si.EndInvoke(iar);

            lock (dicoLock)
            {
                InvokeStackTraces.Remove(InvokeStackTraces.Count - 1);
                if (InvokeStackTraces.Count == 0)
                {
                    InvokeStackTraces.Remove(pid);
                }
            }
        }
Example #11
0
 public static void raiseEvent <T>(EventHandler <T> theEvent, object sender, T args) where T : EventArgs
 {
     if (theEvent == null)
     {
         return;
     }
     foreach (EventHandler <T> invocation in theEvent.GetInvocationList())
     {
         try
         {
             ISynchronizeInvoke target = invocation.Target as ISynchronizeInvoke;
             if (target == null || !target.InvokeRequired)
             {
                 invocation(sender, args);
             }
             else
             {
                 target.BeginInvoke((Delegate)invocation, new object[2]
                 {
                     sender, (object)args
                 });
             }
         }
         //Kệ mẹ lỗi
         catch (Exception ex)
         {
         }
     }
 }
Example #12
0
 internal static void Execute(Action action)//, params object[] args
 {
     if (_sync != null)
     {
         _sync.BeginInvoke(action, null);
     }
 }
Example #13
0
        /// <summary>
        /// Helper method to fire events in a thread safe manner.
        /// </summary>
        /// <remarks>
        /// Checks whether subscribers implement <see cref="System.ComponentModel.ISyncronizeInvoke"/> to ensure we raise the
        /// event on the correct thread.
        /// </remarks>
        /// <param name="mainHandler">The <see cref="System.EventHandler"/> for the event to be raised.</param>
        /// <param name="e">An <see cref="System.EventArgs"/> to be passed with the event invocation.</param>
        private void FireEvent(EventHandler mainHandler, EventArgs e)
        {
            // Make sure we have some subscribers
            if (mainHandler != null)
            {
                // Get each subscriber in turn
                foreach (EventHandler handler in mainHandler.GetInvocationList())
                {
                    // Get the object containing the subscribing method
                    // If the target doesn't implement ISyncronizeInvoke, this will be null
                    ISynchronizeInvoke sync = handler.Target as ISynchronizeInvoke;

                    // Check if our target requires an Invoke
                    if (sync != null && sync.InvokeRequired)
                    {
                        // Yes it does, so invoke the handler using the target's BeginInvoke method, but wait for it to finish
                        // This is preferable to using Invoke so that if an exception is thrown its presented
                        // in the context of the handler, not the current thread
                        IAsyncResult result = sync.BeginInvoke(handler, new object[] { this, e });
                        sync.EndInvoke(result);
                    }
                    else
                    {
                        // No it doesn't, so invoke the handler directly
                        handler(this, e);
                    }
                }
            }
        }
Example #14
0
        private void _nfcReader_TagFound(NFCTag tag)
        {
            // we get called twice for some reason.
            if (_busy)
            {
                return;
            }
            if (ShuttingDown)
            {
                return;
            }
            Log(" _nfcReader_TagFound");

            if (_currentTag != null && _currentTag.UID == tag.UID)
            {
                return;
            }

            _currentTag = tag;

            CurrentDevice = new NFCHoloDevice(_currentTag);
            foreach (Delegate d in DeviceArrived.GetInvocationList())
            {
                ISynchronizeInvoke syncer = d.Target as ISynchronizeInvoke;
                if (syncer != null)
                {
                    syncer.BeginInvoke(d, new NFCHoloDevice[] { CurrentDevice });
                }
                else
                {
                    d.DynamicInvoke(CurrentDevice);
                }
            }
        }
Example #15
0
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler;

            lock (SyncRoot)
                handler = PropertyChanged;

            if (handler != null)
            {
                ISynchronizeInvoke invoker = null;
                lock (SyncRoot)
                {
                    if (_owner != null)
                    {
                        invoker = _owner.Invoker;
                    }
                }
                if (invoker != null && invoker.InvokeRequired)
                {
                    invoker.BeginInvoke(handler, new object[] { this, new PropertyChangedEventArgs(propertyName) });
                }
                else
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
Example #16
0
        /// <summary>
        /// This method calls the given event with the specified parameters either synchronized when the
        /// target and caller thread are the same, or asynchronized if the target and caller are in
        /// different threads.
        /// </summary>
        /// <param name="theEvent">The event to be emitted.</param>
        /// <param name="args">List of parameters passed to the event.</param>
        public static void RaiseEventOnUIThread(Delegate theEvent, object[] args)
        {
            try
            {
                if (theEvent == null)
                {
                    return;
                }

                foreach (Delegate d in theEvent.GetInvocationList())
                {
                    ISynchronizeInvoke syncer = d.Target as ISynchronizeInvoke;
                    if (syncer == null)
                    {
                        d.DynamicInvoke(args);
                    }
                    else
                    {
                        syncer.BeginInvoke(d, args); // cleanup omitted
                    }
                }
            }
            catch
            {
            }
        }
Example #17
0
        private void RaiseEventOnUIThread(Delegate theEvent, string args)
        {
            if (theEvent == null)
            {
                return;
            }

            foreach (Delegate d in theEvent.GetInvocationList())
            {
                ISynchronizeInvoke syncer = d.Target as ISynchronizeInvoke;
                if (syncer == null)
                {
                    try {
                        d.DynamicInvoke(args);
                    }
                    catch { }
                }
                else
                {
                    try
                    {
                        syncer.BeginInvoke(d, new object[] { args });
                    }
                    catch { }
                }
            }
        }
        /// <summary>
        /// Handles exceptions when handling workItems.
        /// </summary>
        protected virtual void OnFailed(WorkItemExceptionEventArgs <T> e)
        {
            if (this.Failed == null)
            {
                return;
            }

            foreach (WorkItemExceptionEventHandler <T> item in this.Failed.GetInvocationList())
            {
                try
                {
                    ISynchronizeInvoke syncItem = (item.Target as ISynchronizeInvoke);
                    if ((syncItem != null) && (syncItem.InvokeRequired))
                    {
                        syncItem.BeginInvoke(item, new object[] { this, e });
                    }
                    else
                    {
                        item(this, e);
                    }
                }
                catch
                {
                }
            }
        }
Example #19
0
        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            ISynchronizeInvoke i = (ISynchronizeInvoke)this;

            if (i.InvokeRequired)
            {
                OnChangeEventHandler tempDelegate = new OnChangeEventHandler(dependency_OnChange);
                object[]             args         = new[] { sender, e };
                i.BeginInvoke(tempDelegate, args);
                return;
            }
            SqlDependency dependency = (SqlDependency)sender;

            dependency.OnChange -= dependency_OnChange;
            changeCount         += 1;
            //this.label5.Text = string.Format(statusMessage, changeCount);
            {
                var withBlock = this.listBox2.Items;
                withBlock.Clear();
                withBlock.Add("Info:   " + e.Info.ToString());
                withBlock.Add("Source: " + e.Source.ToString());
                withBlock.Add("Type:   " + e.Type.ToString());
            }
            GetData();
        }
Example #20
0
        private static void AddTaskDelay(Action action, int delayInMilliseconds)
        {
            System.Threading.Thread.Sleep(delayInMilliseconds);
            bool bFired;

            if (action != null)
            {
                foreach (Delegate singleCast in action.GetInvocationList())
                {
                    bFired = false;
                    try
                    {
                        ISynchronizeInvoke syncInvoke = (ISynchronizeInvoke)singleCast.Target;
                        if (syncInvoke != null && syncInvoke.InvokeRequired)
                        {
                            bFired = true;
                            syncInvoke.BeginInvoke(singleCast, null);
                        }
                        else
                        {
                            bFired = true;
                            singleCast.DynamicInvoke(null);
                        }
                    }
                    catch (Exception)
                    {
                        if (!bFired)
                        {
                            singleCast.DynamicInvoke(null);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Handles end of handling a set of workItems.
        /// </summary>
        protected virtual void OnIdle(EventArgs e)
        {
            if (this.Idle == null)
            {
                return;
            }

            foreach (EventHandler item in this.Idle.GetInvocationList())
            {
                try
                {
                    ISynchronizeInvoke syncItem = (item.Target as ISynchronizeInvoke);
                    if ((syncItem != null) && (syncItem.InvokeRequired))
                    {
                        syncItem.BeginInvoke(item, new object[] { this, e });
                    }
                    else
                    {
                        item(this, e);
                    }
                }
                catch
                {
                }
            }
        }
        /// <summary>
        /// Special Invoke method that operates similarly to <see cref="ISynchronizeInvoke.Invoke"/> but in addition to that, it can be
        /// aborted by setting the stopEvent. This avoids potential deadlocks that are possible when using the regular
        /// <see cref="ISynchronizeInvoke.Invoke"/> method.
        /// </summary>
        /// <param name="method">A <see cref="System.Delegate"/> to a method that takes parameters of the same number and type that are
        /// contained in <paramref name="args"/></param>
        /// <param name="args">An array of type <see cref="System.Object"/> to pass as arguments to the given method. This can be null if
        /// no arguments are needed </param>
        /// <returns>The <see cref="Object"/> returned by the asynchronous operation</returns>
        private object StoppableInvoke(Delegate method, object[] args)
        {
            IAsyncResult asyncResult = invoker.BeginInvoke(method, args);

            waitHandles[0] = asyncResult.AsyncWaitHandle;
            return((WaitHandle.WaitAny(waitHandles) == 0) ? invoker.EndInvoke(asyncResult) : null);
        }
Example #23
0
        private void OnDataArrival(long instanceID, string itemID, LogRowDataPoint[] points)
        {
            if (m_SynchronizeInvoke.InvokeRequired)
            {
                m_SynchronizeInvoke.BeginInvoke(new QueuedReadRequestEventHandler(OnDataArrival), new object[] { instanceID, itemID, points });
                return;
            }

            if (m_Database == null || m_Database.InstanceID != instanceID)
            {
                return;
            }

            foreach (LogRowDataPoint point in points)
            {
                AddPoint(point.TimeStamp.ToOADate(), point.Value);
            }

            m_Pane.AxisChange();

            var startPoint = (float)Math.Max(m_Pane.XAxis.Scale.ReverseTransform((float)points[0].TimeStamp.ToOADate()) - 1, m_Pane.Rect.Left);
            var endPoint   = (float)Math.Min(m_Pane.XAxis.Scale.ReverseTransform((float)points[points.Length - 1].TimeStamp.ToOADate()) + 2, m_Pane.Rect.Right);

            var top    = m_Pane.Rect.Top;
            var bottom = m_Pane.Rect.Bottom;

            RefreshParent(new RectangleF(startPoint, top, endPoint - startPoint, bottom));
        }
Example #24
0
        private static void AddTaskDelay(Delegate ev, object[] paramArray, int time)
        {
            System.Threading.Thread.Sleep(time);
            bool bFired;

            if (ev != null)
            {
                foreach (Delegate singleCast in ev.GetInvocationList())
                {
                    bFired = false;
                    try
                    {
                        ISynchronizeInvoke syncInvoke = (ISynchronizeInvoke)singleCast.Target;
                        if (syncInvoke != null && syncInvoke.InvokeRequired)
                        {
                            bFired = true;
                            syncInvoke.BeginInvoke(singleCast, paramArray);
                        }
                        else
                        {
                            bFired = true;
                            singleCast.DynamicInvoke(paramArray);
                        }
                    }
                    catch (Exception)
                    {
                        if (!bFired)
                        {
                            singleCast.DynamicInvoke(paramArray);
                        }
                    }
                }
            }
        }
Example #25
0
        private void RaiseEvent(Delegate ev, EventArgs arg, EventType evtype)
        {
            if (ev == null)
            {
                return;
            }

            if (synchronizingObject == null)
            {
                foreach (var target in ev.GetInvocationList())
                {
                    switch (evtype)
                    {
                    case EventType.RenameEvent:
                        ((RenamedEventHandler)target).Invoke(this, (RenamedEventArgs)arg);
                        break;

                    case EventType.ErrorEvent:
                        ((ErrorEventHandler)target).Invoke(this, (ErrorEventArgs)arg);
                        break;

                    case EventType.FileSystemEvent:
                        ((FileSystemEventHandler)target).Invoke(this, (FileSystemEventArgs)arg);
                        break;
                    }
                }
                return;
            }

            synchronizingObject.BeginInvoke(ev, new object [] { this, arg });
        }
Example #26
0
        /// <summary>
        /// Invokes all nonsynchronized handlers on new thread (all on one).
        /// </summary>
        /// <param name="handler"></param>
        /// <param name="parameters"></param>
        public static void BeginInvoke(Delegate handler, params object[] parameters)
        {
            if (handler != null)
            {
                List <Delegate> list = new List <Delegate>();

                foreach (Delegate d in handler.GetInvocationList())
                {
                    ISynchronizeInvoke sync = d.Target as ISynchronizeInvoke;
                    if (sync != null)
                    {
                        sync.BeginInvoke(d, parameters);
                    }
                    else
                    {
                        list.Add(d);
                    }
                }

                if (list.Count > 0)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncInvokeListProc), new AsyncInvokeArgs(list, parameters));
                }
            }
        }
Example #27
0
        protected void OnChanged(FileSystemEventArgs e)
        {
            if (Changed == null)
            {
                return;
            }

            if (synchronizingObject == null)
            {
                Changed(this, e);
            }
            else
            {
                synchronizingObject.BeginInvoke(Changed, new object[] { this, e });
            }
        }
Example #28
0
        private void RaiseEvent(Delegate ev, EventArgs arg, EventType evtype)
        {
            if (ev == null)
            {
                return;
            }

            if (synchronizingObject == null)
            {
                switch (evtype)
                {
                case EventType.RenameEvent:
                    ((RenamedEventHandler)ev).BeginInvoke(this, (RenamedEventArgs)arg, null, null);
                    break;

                case EventType.ErrorEvent:
                    ((ErrorEventHandler)ev).BeginInvoke(this, (ErrorEventArgs)arg, null, null);
                    break;

                case EventType.FileSystemEvent:
                    ((FileSystemEventHandler)ev).BeginInvoke(this, (FileSystemEventArgs)arg, null, null);
                    break;
                }
                return;
            }

            synchronizingObject.BeginInvoke(ev, new object [] { this, arg });
        }
Example #29
0
        protected static void InvokeExternalTarget(Delegate dlg, params object[] objList)
        {
            try
            {
                // make sure there are delegates assigned...
                Delegate[] invocationList = dlg.GetInvocationList();
                if (invocationList == null)
                {
                    return;
                }

                // we can only call one delegate at the time...
                foreach (Delegate singleDelegate in invocationList)
                {
                    ISynchronizeInvoke synchronizeTarget = singleDelegate.Target as ISynchronizeInvoke;
                    if (synchronizeTarget == null)
                    {
                        singleDelegate.DynamicInvoke(objList);
                    }
                    else
                    {
                        synchronizeTarget.BeginInvoke(singleDelegate, objList);
                    }
                }
            }
            catch { }
        }
Example #30
0
        /// <summary>
        /// Fires the event passed in in a thread-safe way.
        /// </summary><remarks>
        /// This method loops through the targets of the event and invokes each in turn. If the
        /// target supports ISychronizeInvoke (such as forms or controls) and is set to run
        /// on a different thread, then we call BeginInvoke to marshal the event to the target
        /// thread. If the target does not support this interface (such as most non-form classes)
        /// or we are on the same thread as the target, then the event is fired on the same
        /// thread as this is called from.
        /// </remarks>
        public static void raiseEvent <T>(EventHandler <T> theEvent, object sender, T args) where T : EventArgs
        {
            // Is the event set up?
            if (theEvent == null)
            {
                return;
            }

            // We loop through each of the delegate handlers for this event. For each of
            // them we need to decide whether to invoke it on the current thread or to
            // make a cross-thread invocation...
            foreach (EventHandler <T> handler in theEvent.GetInvocationList())
            {
                try {
                    ISynchronizeInvoke target = handler.Target as ISynchronizeInvoke;
                    if (target == null || target.InvokeRequired == false)
                    {
                        // Either the target is not a form or control, or we are already
                        // on the right thread for it. Either way we can just fire the
                        // event as normal...
                        handler(sender, args);
                    }
                    else
                    {
                        // The target is most likely a form or control that needs the
                        // handler to be invoked on its own thread...
                        target.BeginInvoke(handler, new[] { sender, args });
                    }
                } catch (Exception) {
                    // The event handler may have been detached while processing the events.
                    // We just ignore this and invoke the remaining handlers.
                }
            }
        }
 public static void BeginInvoke(ISynchronizeInvoke synchronizeInvoke, Action action)
 {
     if (synchronizeInvoke.InvokeRequired)
     {
         synchronizeInvoke.BeginInvoke(action, null);
     }
     else
     {
         action();
     }
 }
 public static void BeginSetProperty(ISynchronizeInvoke context, string key, object value)
 {
     context.BeginInvoke(DelegateSetProperty, new Object[] { context, key, value });
 }
Example #33
0
 public static void BeginInvoke(ISynchronizeInvoke syncronizer, Action method)
 {
     syncronizer.BeginInvoke(method, new object[0]);
 }