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

Thread: Scheduled Services..

Hybrid View

  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 offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,274

    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 offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,274

    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 offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,274

    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
    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.

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

    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).

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)