I mean a program based on events, a "static" program that don't just do a task but waits for events, etc. and doesn't end until the user manually close it.
EDIT: I have answered below, for example the programs we use every day and are based in Windows, like Microsoft Word, Firefox, etc. What is this type of program called? How is it possible to do something like that?
EDIT 2: I was going to reply individually some answers, but I will better reply here.
The program I want to do is something like a spider that works in a VPS as a daemon. When it starts it should look if there are tasks to do. If so it will generate the necessary threads (It's also thread based), so the main function needs an infinite loop based on events.
These programs are normally written around what is called an "event loop".
The main function of the program is generally, in psuedocode, something like this:
while (!shouldClose()) {
Event e = getEvent();
dispatchEvent(e);
}
Where the dispatchEvent function takes the event e, determines which function(s) it should invoke, and then calls those functions with any parameters for the event.
The getEvent function can be any number of things depending on the nature of the program. For an interactive command-line program, it might simply be retrieving and parsing a line of text from the user. For a GUI program, it might be waiting for mouse clicks or other messages from the windowing system. For a network service, it might be waiting for incoming packets.
For example, in a GUI program, getEvent might wait for and then receive a mouse click message from the OS windowing system. Then dispatchEvent would look at the mouse click event, determine based on the coordinates which button was clicked on, and then look up the object corresponding to that button, and invoke buttonObject.clicked(). The implementation of buttonObject.clicked is then responsible for executing whatever code should be the result of clicking on the button. Once it is done running, control returns to the event loop, and the next event is processed.
Now, obviously, for GUI programs this can get rather complicated, and so you generally don't write your own event loops from scratch. Instead, programmers often use a graphical framework that provides such a loop for you, and then only fill in the event response code. For command-line programs and network services, events are a bit simpler to process, and often the loop is written from the ground up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With