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 t开发者_JAVA百科here 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.
Since your description is quite vague, it could mean several things:
- a daemon or Windows service
- a shell
- a server
- ...
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.
Simply put, it's called an "Event-driven Program".
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.
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.
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
).
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
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
精彩评论