0

I am trying to create a simple GTK application. I would like to write things on the GUI as soon as certain files are created by different processes. I guess I cannot do a while(true) in the parent process, because that would freeze the GUI. Instead, I am trying to fork, check if files are present and manipulate the GUI, but it does not seem to work, my clumsy code is the following:

int check_for_files(){ FILE *fp; int files_set; char ch; files_set = 0; while(!files_set){ fp = fopen("file", "r"); if(fp != NULL){ content_length = 0; while(ch=getc(fp) != EOF){ content_length++; } fclose(fp); files_set = 1; return files_set; } else{ files_set = 0; } } return 1; } int main(int argc, char *argv[]) { // Main function the get the application UI GtkWidget *window; GdkPixbuf *icon; GtkWidget *grid; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size(GTK_WINDOW(window), 500, 300); gtk_container_set_border_width(GTK_CONTAINER(window), 15); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); int pid = fork(); if (pid == 0){ int files_set = check_for_files(); printf("files outside: %d\n",files_set); gtk_window_set_title(GTK_WINDOW(window), "Files are present!"); } else{ icon = create_pixbuf("net.png"); gtk_window_set_icon(GTK_WINDOW(window), icon); // Get the grid get_grid(window); gtk_widget_show_all(window); g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); g_object_unref(icon); gtk_main(); } } 
1
  • You should do all GUI updates in the main thread. If you detect some change in another thread. For this purpose, g_idle_add() is your friend. Commented Dec 18, 2017 at 10:30

1 Answer 1

3

When you want to perform I/O operations, give a look to gio, which is integrated to GLib. So you should use a GFileMonitor which uses inotify to be notified of filesystem changes. The "Description" section of GFileMonitor presents the functions to use.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.