Example #1
0
        /// <summary>
        /// {@inheritDoc}
        /// </summary>
        public virtual bool Enter()
        {
            if (Log.isLoggable(PlatformLogger.Level.FINE))
            {
                Log.fine("enter(): blockingEDT=" + KeepBlockingEDT.Get() + ", blockingCT=" + KeepBlockingCT.Get());
            }

            if (!KeepBlockingEDT.CompareAndSet(false, true))
            {
                Log.fine("The secondary loop is already running, aborting");
                return(false);
            }

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final Runnable run = new Runnable()
            Runnable run = new RunnableAnonymousInnerClassHelper(this);

            // We have two mechanisms for blocking: if we're on the
            // dispatch thread, start a new event pump; if we're
            // on any other thread, call wait() on the treelock

            Thread currentThread = Thread.CurrentThread;

            if (currentThread == DispatchThread)
            {
                if (Log.isLoggable(PlatformLogger.Level.FINEST))
                {
                    Log.finest("On dispatch thread: " + DispatchThread);
                }
                if (Interval != 0)
                {
                    if (Log.isLoggable(PlatformLogger.Level.FINEST))
                    {
                        Log.finest("scheduling the timer for " + Interval + " ms");
                    }
                    Timer.Schedule(TimerTask = new TimerTaskAnonymousInnerClassHelper(this), Interval);
                }
                // Dispose SequencedEvent we are dispatching on the the current
                // AppContext, to prevent us from hang - see 4531693 for details
                SequencedEvent currentSE = KeyboardFocusManager.CurrentKeyboardFocusManager.CurrentSequencedEvent;
                if (currentSE != null)
                {
                    if (Log.isLoggable(PlatformLogger.Level.FINE))
                    {
                        Log.fine("Dispose current SequencedEvent: " + currentSE);
                    }
                    currentSE.Dispose();
                }
                // In case the exit() method is called before starting
                // new event pump it will post the waking event to EDT.
                // The event will be handled after the the new event pump
                // starts. Thus, the enter() method will not hang.
                //
                // Event pump should be privileged. See 6300270.
                AccessController.doPrivileged(new PrivilegedActionAnonymousInnerClassHelper(this, run));
            }
            else
            {
                if (Log.isLoggable(PlatformLogger.Level.FINEST))
                {
                    Log.finest("On non-dispatch thread: " + currentThread);
                }
                lock (TreeLock)
                {
                    if (Filter != null)
                    {
                        DispatchThread.AddEventFilter(Filter);
                    }
                    try
                    {
                        EventQueue eq = DispatchThread.EventQueue;
                        eq.PostEvent(new PeerEvent(this, run, PeerEvent.PRIORITY_EVENT));
                        KeepBlockingCT.Set(true);
                        if (Interval > 0)
                        {
                            long currTime = DateTimeHelperClass.CurrentUnixTimeMillis();
                            while (KeepBlockingCT.Get() && ((ExtCondition != null) ? ExtCondition.Evaluate() : true) && (currTime + Interval > DateTimeHelperClass.CurrentUnixTimeMillis()))
                            {
                                Monitor.Wait(TreeLock, TimeSpan.FromMilliseconds(Interval));
                            }
                        }
                        else
                        {
                            while (KeepBlockingCT.Get() && ((ExtCondition != null) ? ExtCondition.Evaluate() : true))
                            {
                                TreeLock.Wait();
                            }
                        }
                        if (Log.isLoggable(PlatformLogger.Level.FINE))
                        {
                            Log.fine("waitDone " + KeepBlockingEDT.Get() + " " + KeepBlockingCT.Get());
                        }
                    }
                    catch (InterruptedException e)
                    {
                        if (Log.isLoggable(PlatformLogger.Level.FINE))
                        {
                            Log.fine("Exception caught while waiting: " + e);
                        }
                    }
                    finally
                    {
                        if (Filter != null)
                        {
                            DispatchThread.RemoveEventFilter(Filter);
                        }
                    }
                    // If the waiting process has been stopped because of the
                    // time interval passed or an exception occurred, the state
                    // should be changed
                    KeepBlockingEDT.Set(false);
                    KeepBlockingCT.Set(false);
                }
            }

            return(true);
        }