2

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.

1
  • 1
    You have described pretty well any GUI driven program,system service, server or daemon. The ones which perform a single task and exit are often called 'utilities' to differentiate them from the others. Jun 25, 2010 at 12:24

8 Answers 8

11

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.

5
  • Good point! I also would add a system sleep, to avoid 100% CPU Usage with the semi-infinite loop. I have edited the main post. Thanks.
    – NeDark
    Jun 27, 2010 at 2:09
  • You don't normally need an explicit call to sleep in this sort of a program, because usually getEvent will call a blocking function (such as select or read) that will suspend the program until new events (new data) are available. This is preferred to explicitly calling sleep and checking for events periodically because your program can respond pretty much immediately, and doesn't waste any time checking for data that is not there. Jun 27, 2010 at 3:24
  • OK, then the getEvent() function would be a function of the nature of gets(), that wait the user to input to perform. Thanks for clearing this.
    – NeDark
    Jun 27, 2010 at 3:55
  • Yeah, getEvent in my example is a generic name for a function that takes whatever action is necessary to get the input your program needs, whether it is windowing events (e.g. mouse clicks), typed characters, network packets, piped information from other programs, etc, etc. This is nearly always a blocking operation unless your program has something else useful to do during its "idle" time. Jun 27, 2010 at 12:08
  • In many network-based daemon processes, the blocking (saving your CPU from running full speed) calls include select(), and other Socket Api calls.
    – Warren P
    Jun 28, 2010 at 14:12
5

Since your description is quite vague, it could mean several things:

If you give more details, we can limit the possibilities further.

Update:

A program like Firefox or Word can be called an application. These are typically developed using application frameworks, written in various programming languages.

2

Simply put, it's called an "Event-driven Program".

1
  • That's what I was looking at the beggining. Now I can find a lot of information of this kind of programs, Thanks.
    – NeDark
    Jun 27, 2010 at 4:00
2

I think what you are trying to ask is what you call a program that doesn't run to completion. It is referred to as a "blocking" program. This is because it blocks execution while it waits for external events or inputs which cause it to unblock and perform some work.

The "how" depends on what you are waiting for. If you are waiting for user input, then the GUI is usually blocked on reading an event queue that unblocks when mouse and keyboard events occur. If you are waiting for network data then you are blocked waiting on a socket call or possibly a select call (a means to monitor activity on multiple descriptors). If you are waiting on another process you could be blocked on reading a pipe or a queue or possibly an inter-process semaphore.

These mechanisms suspend execution by not scheduling the blocked thread for execution until the condition that unblocks the particular operation becomes true.

2

I would say "Application", or "Service", depending on whether or not there is a GUI or not.

How is it possible to do something like that

Since you tagged your question with C++, I'll assume you want to use C++ to build a windows application, like Microsoft Word or Excel?

You could download the free Microsoft Visual Studio C++ Express, read some tutorials, buy a book, and start to learn how to write applications. An application like Excel was written over a period of over twenty years, by a team of thousands of developers.

There are many different ways to write Applications for Windows, and many people use languages other than C++ to write these, although C++ is certainly very popular for commercial applications, and I believe Excel and Word are written in a mix of C++ and C.

Here on StackOverflow I count over two dozen different languages used to write applications for Windows, including Python, C++, Java, Delphi, and many many more.

The main structure of such an application looks like this, in python-like-pseudo-code, not very different from the other answer's C-like-code:

initialize_things()

while (!AreWeStoppingYet) :
     event = GetAnEvent()
     ProcessAnEvent(event)

Sometimes this little loop above is part of a "framework" as someone else said. In other words, I never actually write this code myself, I have it written for me. I use Delphi a lot, and this little loop is part of a little object called Application. On many frameworks, including Delphi, and some C++ frameworks, this loop is buried somewhere in the source code for your framework, and you might not even really think about the fact that it is there.

2
  • Good answer. In my cose I am doing a simple linux daemon. I have edited the main post. Thanks.
    – NeDark
    Jun 27, 2010 at 2:10
  • A far better way to ask your question is "How do I get started writing a daemon process in a Unix-like operating system, like Linux?".
    – Warren P
    Jun 28, 2010 at 14:11
1

Well, you just have to have a way for the program to receive the events, such as:

  • keyboard input.
  • connection coming over the net (sockets and such).
  • other IPC mechanisms (shared memory, semaphores, signals).

There are a number of ways this can be done.

Then you run it in the foreground (if you want the user to be able to stop it easily) or as a background process under the various operating systems (UNIX daemon or Windows service, or a background task using UNIX & or Windows start).

1

That kind of program is usually called as having a GUI.

There are many libraries around to help you write a GUI -- in the open source world, there is for example GTK, which works on many platforms including Windows and you can use it in both C and C++.

To help you get started, there is a "Hello World" here : http://library.gnome.org/devel/gtk-tutorial/stable/c39.html#SEC-HELLOWORLD

3
  • When you start Microsoft Word, it waits for events (file open, keyboard etc) and it doesn't go away until the user manually closes it . Yet Word is not a service. Jun 25, 2010 at 12:22
  • @Pete Kirkham: true, it's kind of hard to tell what is being asked here. Edited answer a bit.
    – houbysoft
    Jun 25, 2010 at 12:24
  • The example of Work is good. How is called that type of programs? How is posible to do something like that? Thanks.
    – NeDark
    Jun 25, 2010 at 12:53
1

These programs typically run in the event loop, waiting for an event before reacting accordingly. As mentioned earlier, this is not limited to GUI applications, although they are a good example of it.

A chat server program is a simple example of a non-GUI program that runs persistently. It simply waits idly until it receives a message, which it then has to push to all of its connected clients. Receiving the message is the event trigger, which then causes the program to take a course of action. In a GUI program, the event trigger is often a mouse click or a button press, to which the program reacts.

Videogames also take advantage of the event loop in a sense... while the game isn't over they have to update the world/NPCs and redraw the frame 60 times per second while waiting for user input to update the character

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.