private static void dispatchAllInvocationEventsImpl() {
    assertIsEdt();

    EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();

    MethodHandle dispatchEventMethod = EDT.dispatchEventMethod;
    if (dispatchEventMethod == null) {
      try {
        Method method = EventQueue.class.getDeclaredMethod("dispatchEvent", AWTEvent.class);
        method.setAccessible(true);
        dispatchEventMethod = MethodHandles.lookup().unreflect(method);
      }
      catch (NoSuchMethodException | IllegalAccessException e) {
        throw new RuntimeException();
      }

      EDT.dispatchEventMethod = dispatchEventMethod;
    }
    boolean threadsDumped = false;
    for (int i = 1; ; i++) {
      AWTEvent event = eventQueue.peekEvent();
      if (event == null) break;
      try {
        event = eventQueue.getNextEvent();
        if (event instanceof InvocationEvent) {
          dispatchEventMethod.bindTo(eventQueue).invoke(event);
        }
      }
      catch (Throwable e) {
        ExceptionUtilRt.rethrowUnchecked(e);
        throw new RuntimeException(e);
      }

      if (i % 10000 == 0) {
        //noinspection UseOfSystemOutOrSystemErr
        System.out.println("Suspiciously many (" + i + ") AWT events, last dispatched " + event);
        if (!threadsDumped) {
          threadsDumped = true;
          // todo temporary hack to diagnose hanging builds
          try {
            Object application =
              ReflectionUtil.getMethod(Class.forName("com.intellij.openapi.application.ApplicationManager"), "getApplication")
                .invoke(null);
            System.err.println("Application=" + application + "\n" + ThreadDumper.dumpThreadsToString());
          }
          catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
      }
    }
  }
private static void dispatchAllInvocationEventsImpl()