CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Scheduled Services..

  1. #1
    Join Date
    Feb 2008
    Posts
    61

    Question Scheduled Windows Services..

    Hi,

    I am trying to create a scheduled windows service application with timers which writes periodically to a file..and I am using timers for that..

    But the code dosent seem to work..

    Here's a snippet..




    Code:
    private: System::Windows::Forms::Timer^ timer1;
     
    
    virtual void OnStart(array<String^>^ args) override
    
    {
    
    // TODO: Add code here to start your service.
    
    this->timer1 = gcnew System::Windows::Forms::Timer();
    
    this->timer1->Tick += gcnew System::EventHandler(this, &SRMWindowsServiceWinService::timer1_Tick); 
    
    this->timer1->Enabled = true;
    
    this->timer1->Interval = 1000;
    
    timer1->Start(); 
    
    }
    
     
    
     
    
    private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) 
    
    {
    
    this->myFunc("Start");
    
    }
    
     
    
     
    
    void myFunc(String^ str)
    
    {
    
    FileStream^ fs = gcnew FileStream("C:\\som.txt", FileMode::Append);
    
    StreamWriter^ sw = gcnew StreamWriter(fs);
    
    sw->WriteLine(str + " " + System::DateTime::Now.ToLongTimeString());
    
    sw->WriteLine("_______________________________________________");
    
    sw->Flush();
    
    sw->Close();
    
    fs->Close();
    
    }

    Is something wrong with the timer??



    Writing the same code in a forms application invokes the timer and the function is called properly at the specified interval..

    But this however dosent seem to work in windows service..Why??

    Please help..
    Last edited by somu0915; April 8th, 2008 at 05:16 AM.

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,284

    Re: Scheduled Services..

    Use the EventLog property and a try/catch block to write out the error to the event log.

    I suspect that the account you are running the service under doesn't have permissions to write to the file system.

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Scheduled Services..

    Have you tried debugging the Windows Service. In case you are not sure how to do that, look at this article on Codeproject which shows how to debug a service in VS.NET.
    http://www.codeproject.com/KB/dotnet...nServices.aspx

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,284

    Re: Scheduled Services..

    Quote Originally Posted by Shuja Ali
    Have you tried debugging the Windows Service. In case you are not sure how to do that, look at this article on Codeproject which shows how to debug a service in VS.NET.
    http://www.codeproject.com/KB/dotnet...nServices.aspx
    That is a great debugging technique; however, keep in mind that the account the service runs under while debugging (in this manner) is the current user account and may be different than the account the service runs under when started in the SCM.

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Scheduled Services..

    Quote Originally Posted by Arjay
    That is a great debugging technique; however, keep in mind that the account the service runs under while debugging (in this manner) is the current user account and may be different than the account the service runs under when started in the SCM.
    Basically the technique described above attaches a debugger to the already executing service in the SCM. This way we can be sure that the User Account that is used to actually run the service will be the same as the one that is being debugged.

  6. #6
    Arjay's Avatar
    Arjay is online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,284

    Re: Scheduled Services..

    It doesn't attach the debugger to a service running under the SCM, it just runs the code in debug mode similar to a console app.

    Here's the relevant code from the article.

    Code:
    // The main entry point for the process
    static void Main()
    {
    #if (!DEBUG)
        System.ServiceProcess.ServiceBase[] ServicesToRun;
        ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    #else
        // Debug code: this allows the process to run as a non-service.
        // It will kick off the service start point, but never kill it.
        // Shut down the debugger to exit
        Service1 service = new Service1();
        service.<Your Service's Primary Method Here>();
        // Put a breakpoint on the following line to always catch
        // your service when it has finished its work
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    #endif 
    }

  7. #7
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Scheduled Services..

    Quote Originally Posted by Arjay
    It doesn't attach the debugger to a service running under the SCM, it just runs the code in debug mode similar to a console app.
    I have done lot of debugging on Windows Services and I am pretty sure that it actually halts the Service running in the SCM and lets you debug the service. The only condition here would be that both the Service that is being installed in the SCM and the Service that is currently open in the VS would have to be same.

    If you read the note in this article on MSDN, you will see that it talks about WinLogon service.

  8. #8
    Arjay's Avatar
    Arjay is online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,284

    Re: Scheduled Services..

    Quote Originally Posted by Shuja Ali
    I have done lot of debugging on Windows Services and I am pretty sure that it actually halts the Service running in the SCM and lets you debug the service. The only condition here would be that both the Service that is being installed in the SCM and the Service that is currently open in the VS would have to be same.

    If you read the note in this article on MSDN, you will see that it talks about WinLogon service.
    I disagree. Read the comments in the code - they tell you that the code is running as a 'non-service'.

    Code:
    // Debug code: this allows the process to run as a non-service.
    // It will kick off the service start point, but never kill it.
    // Shut down the debugger to exit
    Your second link (ie. the MSDN one) talks about attaching to a running service to debug, but the first link does not.

  9. #9
    Join Date
    Feb 2008
    Posts
    61

    Question Re: Scheduled Services..

    Quote Originally Posted by Shuja Ali
    Have you tried debugging the Windows Service. In case you are not sure how to do that, look at this article on Codeproject which shows how to debug a service in VS.NET.
    http://www.codeproject.com/KB/dotnet...nServices.aspx
    Well I found out what was wrong with my windows service..
    Microsoft has a bug with System::Timer class and it might not run properly..
    http://geekswithblogs.net/gavin/arch.../01/10546.aspx
    http://weblogs.asp.net/sibrahim/arch.../13/58429.aspx

    So I have used the System::Threading::Timer and it runs fine..

    Code:
    virtual void OnStart(array<String^>^ args) override
    {
    TimerCallback^ tcb = gcnew TimerCallback(this, &SRMWindowsServiceWinService::Timer_TimerCallback);
    this->timer1 = gcnew System::Threading::Timer(tcb, NULL, 0, 600*1000);
    }
    This works..

    However I have another question...

    I want to start a process (say noteped) and what I have scanned various articles, it requires the "Allow Service to interact with desktop" to be checked.. What does desktop interaction mean?

    So how to set the checkbox programmaticaly??
    Is it true for all GUI applications which I want to start??

    What if I have a console app? Will that require the checkbox to be set as well??
    Last edited by somu0915; April 8th, 2008 at 08:22 AM.

  10. #10
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Scheduled Services..

    Quote Originally Posted by Arjay
    I disagree. Read the comments in the code - they tell you that the code is running as a 'non-service'.

    Your second link (ie. the MSDN one) talks about attaching to a running service to debug, but the first link does not.
    I see. The confusion is actually on my side. I was writing about the way that I usually use and you were referring to the link that I posted. Hmm, sometimes assumptions can lead to a great confusion.
    Quote Originally Posted by somu0915
    I want to start a process (say noteped) and what I have scanned various articles, it requires the "Allow Service to interact with desktop" to be checked.. What does desktop interaction mean?

    So how to set the checkbox programmaticaly??
    Is it true for all GUI applications which I want to start??

    What if I have a console app? Will that require the checkbox to be set as well??
    Basically a Windows Service does not run in the normal Window Station. It runs in a different Window Station which does not support UI elements and is not interactive. This is the reason why you would see most of thw Windows Services do not have a UI. Now if your service has a UI element or needs to open a a different UI, it needs to be present in the Interactive Windows Station. The "Allow Service to Interact with Desktop" does exactly that. I haven't seen any piece of code that does this automatically. However here is a work around that I just found on codeproject. You will have to see yourself if this willr eally help you.
    http://www.codeproject.com/KB/instal...cedesktop.aspx

  11. #11
    Join Date
    Feb 2008
    Posts
    61

    Question Re: Scheduled Services..

    Ok..
    But logically, is it a good practice to schedule windows services?

  12. #12
    Arjay's Avatar
    Arjay is online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,284

    Re: Scheduled Services..

    Quote Originally Posted by somu0915
    Ok..
    But logically, is it a good practice to schedule windows services?
    What do you mean? The scheduler schedules apps to run. A Windows Service gets started by the system at system startup (if configured to do so).

    There would be no reason to schedule a windows service (since the point of scheduling is to start up or run an app).

  13. #13
    Arjay's Avatar
    Arjay is online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,284

    Re: Scheduled Services..

    Quote Originally Posted by somu0915
    I want to start a process (say noteped) and what I have scanned various articles, it requires the "Allow Service to interact with desktop" to be checked..
    Read Security, services and the interactive desktop in Windows for why this isn't generally a good idea (and not allowed on Vista).

  14. #14
    Join Date
    Feb 2008
    Posts
    61

    Question Re: Scheduled Services..

    Quote Originally Posted by Arjay
    What do you mean? The scheduler schedules apps to run. A Windows Service gets started by the system at system startup (if configured to do so).

    There would be no reason to schedule a windows service (since the point of scheduling is to start up or run an app).

    Yes, I also get the point..
    So can u guide me how I can add an app(as a task) to the scheduler??
    How can I add a task programmaticaly to the windows scheduler??

  15. #15
    Arjay's Avatar
    Arjay is online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,284

    Re: Scheduled Services..

    Quote Originally Posted by somu0915
    So can u guide me how I can add an app(as a task) to the scheduler????
    No, but I can get you started. Run "Start\Control Panel\Schedule Tasks\Add Scheduled Task". You should be able to figure it out from there.
    Quote Originally Posted by somu0915
    How can I add a task programmaticaly to the windows scheduler??
    Search Google for "Programmatically Task Scheduler".

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)