Le réseau pour les développeurs > Forums - Accueil > Visual C# General > Keep a windows form application running on logoff
Poser une questionPoser une question
 

TraitéeKeep a windows form application running on logoff

  • mercredi 4 novembre 2009 02:53felpis Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->

    I have developed a windows application form that works as follows:
    1. On initial load, the user specifies a start/end date and time and time interval to execute a download method (A basic Timer) from an FTP site.
    2. After initialized, with the user input a Due Time is calculated to start the first instance of the timer and from there execute the download method every hour while being on the background. The file being downloaded is directly related with the time of each hour instance (i.e. the filename is identified with the date and time)
    3. If the computer restarts or reboots, the timer will resume on system start up with information that was stored on an event log before restart or reboot (The information being stored is the next date and time value that the timer would had execute if no event was present and is used on form load as new start date an time and continues from there).
    4. The download process will continue as many times as necessary for all the tasks missed (Due time is no longer negative) and resumes the regular timer activity every hour.
    5. The end date and time is reached, event log gets clear and the application is closed.
    The application was working on several tests including rebooting/restart under windows XP. I subsequently run a test on the desired windows platform (Widows Server 2003) and on log off the application treated this event as restart/reboot since I was using WM_QUERYSESSION which I thought it was only for system shutdown event-like.
    Q: Is there any way to keep the application running in the background on log off?
    I know this issue was posted before not only in this forums but many others, but unfortunately the answers didn't quite applied to my requirements and I will clarify those questions to clear a little more my issue:
    1. Why not use Task Scheduler? = As mentioned before the files being downloaded are named for specific date and time, if I schedule a task to run every hour and the system restarts/reboot upon a common event like an update, how to create a log event with the information of the date and time missed so the application will download the file missed on the next instance?
    2. Use a windows service? = I'm not quite familiar with windows services but for what I have researched the user input is console application meaning no User Interface. Not all users are comfortable with using this kind of Interface, they feel better using a windows form like input interface ("User friendly").
    I've been using C# for a month so I'm not too familiar with the language capabilities. Thanks everybody for any kind of feedback that I can get to solve this issue. If it is necessary I can post the complete code which is somehow extensive and I'm pretty sure it can be narrowed down more but again I'm new on C#.

Réponses

  • mercredi 4 novembre 2009 05:07Ron.Whittle Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    1. Why not use Task Scheduler? = As mentioned before the files being downloaded are named for specific date and time, if I schedule a task to run every hour and the system restarts/reboot upon a common event like an update, how to create a log event with the information of the date and time missed so the application will download the file missed on the next instance?
    Write the date/time of the last successful run somewhere. A file, the registry, whatever. When your program starts, read this in and figure out if you missed any.
    2. Use a windows service? = I'm not quite familiar with windows services but for what I have researched the user input is console application meaning no User Interface.
    You can have a UI in a service. One of the machines that I 'own' is running a service with a UI. I've not looked at its code since I never had to make changes to it, but it's there :)

    Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit
    • Marqué comme réponsefelpis mercredi 4 novembre 2009 20:29
    •  
  • mercredi 4 novembre 2009 20:51JeffWask Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Windows Services run in an isolated from the UI for security reasons.  This separation has gotten more distinct with Vista and then 7 which creates issues with having a UI accessible in the service executable.   However, you use service applications with Windows Form interfaces all the time.  Most Anti-Virus applications run as services with a separate forms interface.  SQL Server, Exchange, Firewalls, all this apps run as services but have Windows form configuration interface.

    The simple solution is two separate executables.  One runs as the service and does the work.  One is the configuration interface.   All shared code is in DLL's.  Your configuration/UI application can communicate with the service application using whatever means are appropriate to your application or not communicate at all in. If it just configures the service it could write the settings to a file then trigger a restart of the service to refresh it's configuration
    • Marqué comme réponsefelpis mercredi 4 novembre 2009 21:25
    •  

Toutes les réponses

  • mercredi 4 novembre 2009 05:07Ron.Whittle Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    1. Why not use Task Scheduler? = As mentioned before the files being downloaded are named for specific date and time, if I schedule a task to run every hour and the system restarts/reboot upon a common event like an update, how to create a log event with the information of the date and time missed so the application will download the file missed on the next instance?
    Write the date/time of the last successful run somewhere. A file, the registry, whatever. When your program starts, read this in and figure out if you missed any.
    2. Use a windows service? = I'm not quite familiar with windows services but for what I have researched the user input is console application meaning no User Interface.
    You can have a UI in a service. One of the machines that I 'own' is running a service with a UI. I've not looked at its code since I never had to make changes to it, but it's there :)

    Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit
    • Marqué comme réponsefelpis mercredi 4 novembre 2009 20:29
    •  
  • mercredi 4 novembre 2009 20:06felpis Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Ron:
    Thank you for replying. I will try each of the approaches (Task Scheduling and windows service) to see which one is more suitable for the kind of project I'm working on. I do have a question regarding windows service and this is because I have read in many of the forums that it is not recommended to use a windows form in conjunction with a windows service. Is this correct?.

    Again thank you for taking the time and replying my post and any additional comment will be valuable.
  • mercredi 4 novembre 2009 20:51JeffWask Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Windows Services run in an isolated from the UI for security reasons.  This separation has gotten more distinct with Vista and then 7 which creates issues with having a UI accessible in the service executable.   However, you use service applications with Windows Form interfaces all the time.  Most Anti-Virus applications run as services with a separate forms interface.  SQL Server, Exchange, Firewalls, all this apps run as services but have Windows form configuration interface.

    The simple solution is two separate executables.  One runs as the service and does the work.  One is the configuration interface.   All shared code is in DLL's.  Your configuration/UI application can communicate with the service application using whatever means are appropriate to your application or not communicate at all in. If it just configures the service it could write the settings to a file then trigger a restart of the service to refresh it's configuration
    • Marqué comme réponsefelpis mercredi 4 novembre 2009 21:25
    •  
  • mercredi 4 novembre 2009 21:34felpis Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Thanx Jeff for replying, it definetly cleared my doubts. Unfortunately as I mentioned before I've been using C# for a month so I'm still new at this language. I spend too much time developing what I already have and I need something to show (besides lines and lines of code) so I think I will take the fastest approach using task scheduler at leats for now. But I will use your advise and take a look at windows services more in depth.

    Again thanx for your help.