0

I've written a command line program in C that is invoked by shell scripts. The last API call in displayplacer instantly applies the desired changes but takes up to 10 seconds to return. How can I execute this line without waiting for the result? My intention is to invoke CGCompleteDisplayConfiguration() and then exit the program immediately so that the following lines in the parent shell scripts are not unnecessarily paused for 10 seconds.

Something like this would be ideal:

isSuccess = someCode() && isSuccess;

CGCompleteDisplayConfiguration(configRef, kCGConfigurePermanently); //non-blocking

if (isSuccess) {
    return 0;
} else {
    return 1;
}

Jake Hilborn
  • 311
  • 4
  • 8
  • 5
    Run it in the backgroung – 0___________ Jun 30 '19 at 19:06
  • 2
    If I understand you right, you want your C programme to *exit* while the task is still not completed? If so, you'd need to create a child process ([`fork`](https://linux.die.net/man/2/fork) on linux or `CreateProcess`, if on Windows). Doesn't seem the right approach to me, though. Preferrably send the programme as a whole to background already from your shell (e. g. [linux](https://www.maketecheasier.com/run-bash-commands-background-linux/) or [windows](https://serverfault.com/questions/35305/cmd-exe-how-to-start-a-background-process-run-some-things-stop-background-pro)). – Aconcagua Jun 30 '19 at 19:54
  • Running the entire program in the background isn't ideal since the first few seconds of execution time re-arrange the users monitors on macOS. The last API call is the one I don't want to wait to complete. – Jake Hilborn Jun 30 '19 at 22:28
  • A program which pretends to be done but still does something in the background... Is this a good idea? Immediately, the situation pops in my mind: User is in hurry and starts your program. Program is done. User is satisfied and switchs off the computer... – Scheff's Cat Jul 01 '19 at 07:02
  • I am fully aware this is a hack. I would like to experiment with the approach though. – Jake Hilborn Jul 01 '19 at 17:35
  • MacOS supports `fork` ([example](https://stackoverflow.com/a/4327062/1312382)) as well. Instead of calling `execv` like there, you'd just call the function in question. Just make sure you call it in the child process (result of `fork` == 0) and parent can return afterwards. – Aconcagua Jul 02 '19 at 08:06
  • 1
    Make your program a kernel daemon ? – Dali Jul 02 '19 at 15:46
  • @Aconcagua Your link is helpful, but for some reason doesn't work in this situation. Calling CGCompleteDisplayConfiguration() on the child thread kills the child thread and does nothing. – Jake Hilborn Jul 05 '19 at 19:28

0 Answers0