GUI: add threading

This commit is contained in:
shadeyg56 2023-09-08 14:57:39 -05:00
parent 86fd056687
commit 81bb72fe20
2 changed files with 12 additions and 5 deletions

View File

@ -6,6 +6,7 @@ from gi.repository import Gtk, GLib, Gdk, Gio, GdkPixbuf
import os
import sys
from threading import Thread
sys.path.append("../")
from auto_cpufreq.core import is_running
@ -59,7 +60,7 @@ class ToolWindow(Gtk.Window):
self.hbox.pack_start(self.vbox_right, False, False, 0)
GLib.timeout_add_seconds(5, self.refresh)
GLib.timeout_add_seconds(5, self.refresh_in_thread)
def daemon_not_running(self):
self.box = DaemonNotRunningView(self)
@ -78,9 +79,12 @@ class ToolWindow(Gtk.Window):
self.gtk_context.add_provider_for_screen(screen, self.gtk_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
self.gtk_provider.load_from_file(Gio.File.new_for_path(CSS_FILE))
def refresh(self):
def refresh_in_thread(self):
Thread(target=self._refresh).start()
return True
def _refresh(self):
self.systemstats.refresh()
self.currentgovernor.refresh()
self.cpufreqstats.refresh()
return True

View File

@ -7,6 +7,7 @@ from gi.repository import Gtk, GdkPixbuf
import sys
import os
import platform as pl
from threading import Thread
sys.path.append("../../")
from subprocess import getoutput, run, PIPE
@ -181,7 +182,8 @@ class DropDownMenu(Gtk.MenuButton):
confirm.destroy()
if response == Gtk.ResponseType.YES:
try:
result = run("pkexec auto-cpufreq --remove", shell=True, stdout=PIPE, stderr=PIPE)
# run in thread to prevent GUI from hanging
result = Thread(target=run, args=("pkexec auto-cpufreq --remove",), kwargs={"shell": True, "stdout": PIPE, "stderr": PIPE}).start()
if result.stderr.decode() == PKEXEC_ERROR:
raise Exception("Authorization was cancelled")
dialog = Gtk.MessageDialog(
@ -263,7 +265,8 @@ class DaemonNotRunningView(Gtk.Box):
def install_daemon(self, button, parent):
try:
result = run("pkexec auto-cpufreq --install", shell=True, stdout=PIPE, stderr=PIPE)
# run in thread to prevent GUI from hanging
result = Thread(target=run, args=("pkexec auto-cpufreq --install",), kwargs={"shell": True, "stdout": PIPE, "stderr": PIPE}).start()
if result.stderr.decode() == PKEXEC_ERROR:
raise Exception("Authorization was cancelled")
elif result.stderr is not None: