【问题标题】:GTK application for looping other apps - problem with freeze UI用于循环其他应用程序的 GTK 应用程序 - 冻结 UI 的问题
【发布时间】:2020-05-27 10:48:16
【问题描述】:

我的问题是for loop 与 GUI 应用程序的操作。我正在编写一个程序,通过单击run button(用于测试)打开其他程序,在运行期间我的UI 开始挂起。如何让我的UI 与另一个应用程序同时工作而不会冻结?这样所有元素在 for 循环期间都是可点击的

ma​​in.c - gtk 代码

#include <gtk/gtk.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

GtkWidget *window;
GtkWidget *run_button;
GtkWidget *stop_button;
GtkWidget *button_box;

int main(int argc, char **argv)
{
  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
  gtk_window_set_title(GTK_WINDOW(window), "Application - Launch GUI Tests");
  gtk_window_set_default_size(GTK_WINDOW(window), 600, 480);

  /* create button box - start / stop buttons */
  button_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_box_set_homogeneous(GTK_BOX(button_box), TRUE);

  stop_button = gtk_button_new_with_label("Stop");
  run_button = gtk_button_new_with_label("Run");

  /* button box pack */
  gtk_box_pack_start(GTK_BOX(button_box), stop_button, FALSE, TRUE, 0);
  gtk_box_pack_start(GTK_BOX(button_box), run_button, FALSE, TRUE, 0);
  gtk_container_add(GTK_CONTAINER(window), button_box);

  /* signals */
  g_signal_connect(G_OBJECT(window), "destroy", gtk_main_quit, NULL);
  g_signal_connect(G_OBJECT(run_button), "clicked", G_CALLBACK(on_run_button_clicked), NULL);

  gtk_widget_show_all(window);
  gtk_main();

  return 0;
}

当我点击运行按钮时,这段代码开始工作(如下)

void on_run_button_clicked(GtkWidget *run_button)
{
  char *list[] = {"xfce4-terminal", "gnome-application", "test-app", "firefox"};

  pid_t child;
  int status;

  for (int i = 0; *(list + i); i++) {
    switch(child = fork()) {
      case -1:
        perror("child - fork error");
        exit(EXIT_FAILURE);
      case 0: /* child process exec another program */
        execlp(list[i], list[i], NULL);
        exit(127);
      default: /* the parent waits until the program is closed */
        wait(&status);
        //check_status(status);
    }
  }
}

并且应用程序会依次启动,直到用户关闭它们,但问题是主应用程序开始挂起,stop 按钮不可用。如何解决?请帮帮我

【问题讨论】:

    标签: c for-loop gtk fork exec


    【解决方案1】:

    看起来main() 中的gtk_main() 无法执行其操作,因为on_run_button_clicked() 卡在wait() 中。 您需要确保 on_run_button_clicked() 实际返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多