Hello everyone, I am creating an IDE in C using GTK+. In my IDE, i want to determine whenever the file accessed by another program. For that I used inotify() for POSIX. To determine whether the file is accessed or not, an infinite loop has to executed, which will continuously check whether file has been accessed or not. But, we know running an infinite loop in the main thread will block it. So, I decide to run the loop in another thread and whenever the file will be accessed a message box will be displayed. For that what I did is, I created a struct WatchFile which will watch the file whether it has been accessed or not. Here's the code.watchfile.h

Here, we have different functions. watch_file_add_callback will add a callback function, and will be called whenever the file has been accessed. watch_file_begin will create a new thread and will run watch_file_thread_begin function in the newly created thread using pthread_t in POSIX and whenever the file will be accessed it will execute the callback_function.As I will use it in my IDE, so I created a new watch_file object, initializes it, starts it and add or remove files whenever a file is opened or saved or a tab is removed. But whenever the file is accessed it calls the callback_function and if I try to create a GtkDialog inside that function and run it using gtk_dialog_run() then program hangs. Here's the callback function

I think the problem is that the GtkDialog must run inside a main thread but it is running in another thread, so can you tell me how to solve this problem. I know we have GFileMonitor, but I want to solve this problem.

What I assume you are trying to do is make calls to GTK/GDK from different threads. This you can not do as GTK/GDK is not thread safe. You could use gdk_threads_enter()/gdk_threads_leave() calls, but these are soon to be deprecated so I would avoid these.

The best method is to use an inter process communication of some sorts. Such as g_idle_add() to add to a queue of signals that would be called in the main thread. Note that Glib is thread safe.

Other notes are

- I would prefer to use the GIO functions to monitor file changes, as I would then not be re-inventing the wheel. It would then be portable from system to system. It is much easier to use and the signals can be added to the main loop. The actual file monitoring is done in another thread with GIO, but you do not need to use threads directly.

- You do not say what versions of the libraries you are using, but you are using features that were deprecated many years ago and now have been removed in GTK+ 3

Code:

GTK_DIALOG(dialog)->vbox

is no longer valid use

Code:

GtkWidget * gtk_dialog_get_content_area (GtkDialog *dialog);

instead.

- Since you have included the Glib library (you always will if you use GTK) prefer functions from Glib rather than extension to libraries so use g_strdup_printf() rather than asprintf()

Who is online

Users browsing this forum: No registered users and 6 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum