Beispiel #1
0
def dialog_run(run_fn) -> int:
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import GLib, Gtk
    log("dialog_run(%s) is_main_thread=%s, main_level=%i", run_fn,
        is_main_thread(), Gtk.main_level())
    if is_main_thread() or Gtk.main_level() == 0:
        return run_fn()
    log("dialog_run(%s) main_depth=%s", run_fn, GLib.main_depth())
    #do a little dance if we're not running in the main thread:
    #block this thread and wait for the main thread to run the dialog
    from threading import Event
    e = Event()
    code = []

    def main_thread_run():
        log("main_thread_run() calling %s", run_fn)
        try:
            code.append(run_fn())
        finally:
            e.set()

    GLib.idle_add(main_thread_run)
    log("dialog_run(%s) waiting for main thread to run", run_fn)
    e.wait()
    log("dialog_run(%s) code=%s", run_fn, code)
    return code[0]
Beispiel #2
0
def dialog_run(dialog) -> int:
    from gi.repository import GLib
    if is_main_thread():
        force_focus()
        dialog.show()
        try:
            return dialog.run()
        finally:
            dialog.destroy()
    #do a little dance if we're not running in the main thread:
    #block this thread and wait for the main thread to run the dialog
    from threading import Event
    e = Event()
    code = []
    def main_thread_run():
        force_focus()
        dialog.show()
        try:
            r = dialog.run()
        finally:
            dialog.destroy()
        code.append(r)
        e.set()
    GLib.idle_add(main_thread_run)
    e.wait()
    log("dialog_run(%s) code=%s", dialog, code)
    return code[0]
Beispiel #3
0
    def test_is_main_thread(self):
        assert is_main_thread()
        result = []

        def notmainthread():
            result.append(is_main_thread())

        from threading import Thread
        t = Thread(target=notmainthread)
        t.start()
        t.join()
        assert len(result) == 1
        assert result[0] is False
Beispiel #4
0
 def notmainthread():
     result.append(is_main_thread())
Beispiel #5
0
 def verify_main_thread():
     if not is_main_thread():
         import threading
         log.error("Error: invalid access from thread %s",
                   threading.current_thread())
         traceback.print_stack()