0

I am creating an application in C++ gtk and if I press a button a threading process will start and I need to run the application if the window is closed also is it possible?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
rrama
  • 19
  • 2
  • 4

2 Answers2

0

On Windows, you need to create a Windows/Linux/Mac Service or run the process in background. On Linux you need to create a daemon service or run the process in the background. Services allow to automatically start the process on boot.

vvkatwss vvkatwss
  • 3,345
  • 1
  • 21
  • 24
amit kumar
  • 20,438
  • 23
  • 90
  • 126
0

Under a Unix system (and since Windows 10), you create another process using the fork() function. To run a program you then use the execve() or similar.

However, that means you need to communicate with that other process using a pipe (see pipe() or pipe2()) or via the network.

Using a thread instead of a process allows you to run in the same memory & process and you can very easily shared everything between multiple threads.

As far as I know, the gtk loop just returns once the user selects the "Close Window" or similar exit function. It would be up for your main() function to make sure that it waits for all the threads to be done before exiting. For threads, this is usually done with a "join()". It will depend on the library you use to run your background process.

Note that in most cases people expect processes to exit whenever they ask the process to exit. Showing a window saying that your process is still running in the background (is busy) is a good idea for a process which runs a GUI. Especially, if you run your process from the console, it would not exit immediately after you closed the window, so letting the user know what's happening is important otherwise they are likely to hit Ctrl-C and kill the whole thing.

If you'd like the main to return but be able to keep the background threads running, it's a tad bit more complicated, but it uses both of the solutions I just mentioned:

  1. create a pipe()
  2. fork() (but no execve())
  3. from within the forked app. (child) open Gtk window, background thread, etc.
  4. when last Gtk window is closed, send message over pipe
  5. parent process receives message and quits immediately
  6. child process still attempts a "join()" to wait for the background thread

This way, the background process with threads created in (3) can continue to run (your function still needs to wait for all the threads to end with the "join()" call), however, the use has a sense of "the app. is done" since it returns to the next line on the prompt in your console even though a background process is still running.

The pipe() and wait on a message on the pipe() is not required if you don't mind having your application always running in the background.

Note: that usage of fork() is most often seen when creating processes that want to run in the background (i.e. services, often called servers under Unix). That's how they get their PPID set to 1.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156