Only by looping the main one can solve this problem or is there a more beautiful solution?
- 2Perhaps it makes sense to think in the direction of services? - Aleksandr Zharinov
- 2What kind of events? - PashaPash ♦
- @PashaPash, connect / disconnect external drives. - iluxa1810
- For such purposes, all the same service is better, here is an article on how habrahabr.ru/post/102826 - rdorn
- I’m too lazy to write the answer - so I’ll type in keywords here "RegisterDeviceNotification, WM_DEVICECHANGE, invisible form, Application.Run ()" . - Pavel Mayorov
3 answers
The essence of the application is the execution of the Main function, while this application is supposed to work. Accordingly, if the application should work forever, then not to leave the Main method is the only right and absolutely natural solution.
Don't be fooled by different platforms like Windows.Forms - ultimately your code goes to the Main method in .NET, which goes to the input address in the header of the executable file. When the function completes execution, the axis considers the process complete.
From the point of view of implementation, of course, you should not do while (true) Thread.Sleep(1) , instead of this, you should normally wait for events to which the application should react. For example, if an application should respond to input to the console, then in a loop you can read Console.ReadLine() - this function will stop execution without devouring CPU time and return the line when it is there.
The truth is that in Windows "always-running applications" are usually made services. The service is registered in the system list of services, receives funds for managing the execution by the user, the axis monitors the start and operation of the service, etc. The console is convenient except for debugging, because you can output the log directly to the console for clarity. If the application needs to interact closely with the user, you can also make an icon in the notification area.
The console application can not work in the background. The console is stdin, stdout, and stderr. ANY application running in the background must disable these standard files.
Such an application, in linux terminology, is called a “daemon”. In the screw language - "service".
There are strict rules for writing demons. For example, how they should disconnect from the console. These rules are set out in many places. For example here:
http://citforum.ru/programming/unix/daemons/
Any daemon is idle until some event occurs. Well, for example:
- Daemon received SIGHUP signal
- The daemon received a notification from inotify
- A message has arrived from a channel or message queue ...
- The question asks, obviously, about Windows, and in your answer you talk about programming Unix daemons. - VladD
- @VladD, the platform does not follow from the question. - ixSci
- @ixSci: No, well, formally, of course, it does not follow. But in real life, I didn’t see a happy hyip about C # among Unixoids, so the answer lacks the phrase “In the unlikely event that your target platform is Linux, ...”. - VladD
- about C # I read the question again, but did not see the words "C #" or "Windows" there. Maybe not looking there? Writing a demon in C # is awesome! :-) You can probably try it on PHP ... Maybe, after all, screws - to tighten, and nails - to hammer? - Sergey
- @ Sergey, see what tags the question has (blue rectangles in the question) - ixSci
First, the application running in the background must of course be a service (Windows Service). But it is useful to implement a dual mode - so that you can also run from the command line - needed for checks and debugging.
Secondly, a scheduler is needed - a part of the program that is responsible for invoking tasks (tasks, jobs) on a schedule or on external events. Sheduler is better to take ready (for example, Quartz.net) - so as not to step on all the same rakes again.