.NETGURU
Hourly Check?
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcs' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Alan Flaherty
Hi All,

I need to run a background service that checks a config file on the hour
to see if any site management emails need to be send. If there are it
mails them out.

How and where would I hook up the timing code?

Any pointers or suggestions welcome

TIA

Reply to this message...
 
    
Douglas Reilly (VIP)
When you create a service, you can create a thread and set that thread up to
check that the service should still be running periodically, and also check
to see if it is time to actually do some scheduled task. I have written a
service like this with Win32 (in the book Inside Server-Based Applications),
and doing so in .NET would be easier. You might use an event object to wake
up the thread either when the service needs to terminate or when it is time
to do something.

Doug Reilly
Designing Microsoft(r) ASP.NET Applications
http://www.programmingasp.net

----- Original Message -----
From: "Alan Flaherty" <Click here to reveal e-mail address>
To: "aspngcs" <Click here to reveal e-mail address>
Sent: Sunday, July 07, 2002 10:20 PM
Subject: [aspngcs] Hourly Check?

[Original message clipped]

Reply to this message...
 
    
Wally McClure
I would use the Timer object. It is located in the System.Timers namespace.

Wally

----- Original Message -----
From: "Douglas Reilly" <Click here to reveal e-mail address>
To: "aspngcs" <Click here to reveal e-mail address>
Sent: Monday, July 08, 2002 9:24 PM
Subject: [aspngcs] Re: Hourly Check?

> When you create a service, you can create a thread and set that thread up
to
> check that the service should still be running periodically, and also
check
[Original message clipped]

Reply to this message...
 
    
Brian Boyce
Alan,

I would write a simple console application and schedule it to run with
WinAt, why have the code running continuously if it only required each hour.

If you do want it to run continuously then I would recommend you send the
process to sleep for an hour - haven't done it in .NET but I know there is
an API to send a process to sleep ( I can dig out the code if you are
interested - we used in a datawarhouse application to check for input files
- send me a mail off list if you want this code). I am sure there will be a
way to do the same in .NET.

Hope this helps,
Brian Boyce

-----Original Message-----
From: Alan Flaherty [mailto:Click here to reveal e-mail address]
Sent: 08 July 2002 03:21
To: aspngcs
Subject: [aspngcs] Hourly Check?

Hi All,

I need to run a background service that checks a config file on the hour to
see if any site management emails need to be send. If there are it mails
them out.

How and where would I hook up the timing code?

Any pointers or suggestions welcome

TIA

| [aspngcs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT

Reply to this message...
 
    
Alan Flaherty
Where would I initiate my custom timer class? In the Application_OnStart
of the global.asax or just use an object tag in the global.asax

-----Original Message-----
From: Wally McClure [mailto:Click here to reveal e-mail address]
Sent: 09 July 2002 02:50
To: aspngcs
Subject: [aspngcs] Re: Hourly Check?

I would use the Timer object. It is located in the System.Timers
namespace.

Wally

----- Original Message -----
From: "Douglas Reilly" <Click here to reveal e-mail address>
To: "aspngcs" <Click here to reveal e-mail address>
Sent: Monday, July 08, 2002 9:24 PM
Subject: [aspngcs] Re: Hourly Check?

> When you create a service, you can create a thread and set that thread
up
to
> check that the service should still be running periodically, and also
check
> to see if it is time to actually do some scheduled task. I have
written a
> service like this with Win32 (in the book Inside Server-Based
Applications),
> and doing so in .NET would be easier. You might use an event object
to
wake
> up the thread either when the service needs to terminate or when it is
time
[Original message clipped]

| [aspngcs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT

Reply to this message...
 
    
Wally McClure
Code that wakes up every hour and performs some type of operations does not
exist within a web application. It runs within what is called a Windows
Service. You can create a Windows Service with VS.NET. Inside the service,
you create a .NET Timer object and run it's elapsed event handler there.

Wally

----- Original Message -----
From: "Alan Flaherty" <Click here to reveal e-mail address>
To: "aspngcs" <Click here to reveal e-mail address>
Sent: Tuesday, July 09, 2002 6:07 AM
Subject: [aspngcs] Re: Hourly Check?

[Original message clipped]

Reply to this message...
 
    
Strauss, Jon (VIP)
Hi Alan,

Using the Timer object, you could do something like the following...

namespace MyServices
{
public class Service1 : System.ServiceProcess.ServiceBase
{
    private Timer        m_Timer;    

    public Service1()
    {
        m_Timer = new Timer();
        m_Timer.Interval = myInterval;        
        m_Timer.Elapsed += new ElapsedEventHandler(OnTimer);
    }

    protected void OnTimer(Object source, ElapsedEventArgs e)
    {
        DoStuff();
    }

    // The main entry point for the process
    static void Main()
    {
        System.ServiceProcess.ServiceBase[] ServicesToRun;

        // More than one user Service may run within the same process. To add
        // another service to this process, change the following line to
        // create a second service object. For example,
        //
        //
        ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

        System.ServiceProcess.ServiceBase.Run(ServicesToRun);

    }
}
}

hth,
Jon

[Original message clipped]

Reply to this message...
 
    
Brian W. Spolarich

Just to be painfully explicit here, VS.NET provides a project template =
for creating Windows Service applications.

It produces code like the sample below:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace WindowsService1
{
    public class Service1 : System.ServiceProcess.ServiceBase
    {
        /// <summary>=20
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components =3D null;

        public Service1()
        {
            // This call is required by the Windows.Forms Component Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitComponent call
        }

        // The main entry point for the process
        static void Main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
=09
            // More than one user Service may run within the same process. To add
            // another service to this process, change the following line to
            // create a second service object. For example,
            //
            // ServicesToRun =3D New System.ServiceProcess.ServiceBase[] {new =
Service1(), new MySecondUserService()};
            //
            ServicesToRun =3D new System.ServiceProcess.ServiceBase[] { new =
Service1() };

            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }

        /// <summary>=20
        /// Required method for Designer support - do not modify=20
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components =3D new System.ComponentModel.Container();
            this.ServiceName =3D "Service1";
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components !=3D null)=20
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        /// <summary>
        /// Set things in motion so your service can do its work.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
        }
=20
        /// <summary>
        /// Stop this service.
        /// </summary>
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop =
your service.
        }
    }
}

| -----Original Message-----
| From: Wally McClure [mailto:Click here to reveal e-mail address]
| Sent: Tuesday, July 09, 2002 7:54 AM
| To: aspngcs
| Subject: [aspngcs] Re: Hourly Check?
|=20
|=20
| Code that wakes up every hour and performs some type of=20
| operations does not
| exist within a web application. It runs within what is called=20
| a Windows
| Service. You can create a Windows Service with VS.NET. Inside=20
| the service,
| you create a .NET Timer object and run it's elapsed event=20
| handler there.

Reply to this message...
 
    
FrogBody
Brian,

Great suggestion. Just to be more clear for poor saps like me who
weren't very familiar with WinAt, I found this:

http://www.progress.com/services/support/cgi-bin/techweb-kbase.cgi/kbase
3.w?Sol_Id=18224

Carson

-----Original Message-----
From: Brian Boyce [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 09, 2002 2:01 AM
To: aspngcs
Subject: [aspngcs] RE: Hourly Check?

Alan,

I would write a simple console application and schedule it to run with
WinAt, why have the code running continuously if it only required each
hour.

If you do want it to run continuously then I would recommend you send
the
process to sleep for an hour - haven't done it in .NET but I know there
is
an API to send a process to sleep ( I can dig out the code if you are
interested - we used in a datawarhouse application to check for input
files
- send me a mail off list if you want this code). I am sure there will
be a
way to do the same in .NET.

Hope this helps,
Brian Boyce

-----Original Message-----
From: Alan Flaherty [mailto:Click here to reveal e-mail address]
Sent: 08 July 2002 03:21
To: aspngcs
Subject: [aspngcs] Hourly Check?

Hi All,

I need to run a background service that checks a config file on the hour
to
see if any site management emails need to be send. If there are it mails
them out.

How and where would I hook up the timing code?

Any pointers or suggestions welcome

TIA

| [aspngcs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT

| [aspngcs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT
-

-

Reply to this message...
 
    
Wally McClure
Don't forget to add the install class to the project to properly setup
the service.

Wally

-----Original Message-----
From: Brian W. Spolarich [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 09, 2002 9:28 AM
To: aspngcs
Subject: [aspngcs] Re: Hourly Check?

Just to be painfully explicit here, VS.NET provides a project template
for creating Windows Service applications.

It produces code like the sample below:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace WindowsService1
{
    public class Service1 : System.ServiceProcess.ServiceBase
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components =
null;

        public Service1()
        {
            // This call is required by the Windows.Forms
Component Designer.
            InitializeComponent();

            // TODO: Add any initialization after the
InitComponent call
        }

        // The main entry point for the process
        static void Main()
        {
            System.ServiceProcess.ServiceBase[]
ServicesToRun;
    
            // More than one user Service may run within the
same process. To add
            // another service to this process, change the
following line to
            // create a second service object. For example,
            //
            // ServicesToRun = New
System.ServiceProcess.ServiceBase[] {new Service1(), new
MySecondUserService()};
            //
            ServicesToRun = new
System.ServiceProcess.ServiceBase[] { new Service1() };

    
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }

        /// <summary>
        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new
System.ComponentModel.Container();
            this.ServiceName = "Service1";
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        /// <summary>
        /// Set things in motion so your service can do its
work.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
        }

        /// <summary>
        /// Stop this service.
        /// </summary>
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down
necessary to stop your service.
        }
    }
}

| -----Original Message-----
| From: Wally McClure [mailto:Click here to reveal e-mail address]
| Sent: Tuesday, July 09, 2002 7:54 AM
| To: aspngcs
| Subject: [aspngcs] Re: Hourly Check?
|
|
| Code that wakes up every hour and performs some type of
| operations does not
| exist within a web application. It runs within what is called
| a Windows
| Service. You can create a Windows Service with VS.NET. Inside
| the service,
| you create a .NET Timer object and run it's elapsed event
| handler there.

| [aspngcs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT

Reply to this message...
 
    
Brian W. Spolarich

Jon, I'm a little confused by your example vs. the VS.NET framework =
documentation.

The docs describe two classes: System.Windows.Forms.Timer and =
System.Threading.Timer. I would have imagined that one would use the =
Threading.Timer classes for general-purpose sleep/wake event handling =
like this, however the constructor and methods you've used in your =
example references the System.Windows.Forms.Timer class, which must be =
used in a Windows Forms application.

It is of course confusing to me why the framework has two different =
classes with the same name.

The 'Elapsed' property also doesn't seem to be provided by either =
class.

Basically I can't compile the example code you've supplied. :-)

Regards,

-bws

| -----Original Message-----
| From: Strauss, Jon [mailto:Click here to reveal e-mail address]
| Sent: Tuesday, July 09, 2002 8:16 AM
| To: aspngcs
| Subject: [aspngcs] RE: Hourly Check?
|=20
|=20
| Hi Alan,
|=20
| Using the Timer object, you could do something like the following...
|=20
| namespace MyServices
| {
| public class Service1 : System.ServiceProcess.ServiceBase
| {
|     private Timer        m_Timer;=09
|=20
|     public Service1()
|     {
|         m_Timer =3D new Timer();
|         m_Timer.Interval =3D myInterval;    =09
|         m_Timer.Elapsed +=3D new ElapsedEventHandler(OnTimer);
|     }
|=20
|     protected void OnTimer(Object source, ElapsedEventArgs e)
|     {
|         DoStuff();
|     }
|=20
|     // The main entry point for the process
|     static void Main()
|     {
|         System.ServiceProcess.ServiceBase[] ServicesToRun;
|=20
|         // More than one user Service may run within=20
| the same process. To add
|         // another service to this process, change the=20
| following line to
|         // create a second service object. For example,
|         //
|         //
|         ServicesToRun =3D new=20
| System.ServiceProcess.ServiceBase[] { new Service1() };
|=20
|         System.ServiceProcess.ServiceBase.Run(ServicesToRun);
|=20
|     }
| }
| }

Reply to this message...
 
    
Strauss, Jon (VIP)
Sorry about that Brian. I wasn't really intending it to be a complete example. However, the timer that I'm using is System.Timers.Timer. All you should have to do is create a new "Windows Service" project from within VS.NET. Then I add the timer object, initalize it in the constructor, and then add the timer callback routine (OnTimer).

Here are the namespaces that I'm including, though there are extra ones that you may not need.

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers;

Jon

[Original message clipped]

Reply to this message...
 
    
Ryan Trudelle-Schwarz
Actually there are 3 timers classes in the framework. Likely the
reference to the Elapsed event came from System.Timers.Timer, which, I
think, would be the best class to use in this case.

-> -----Original Message-----
-> From: Brian W. Spolarich [mailto:Click here to reveal e-mail address]
->
-> Jon, I'm a little confused by your example vs. the VS.NET framework
-> documentation.
->
-> The docs describe two classes: System.Windows.Forms.Timer and
-> System.Threading.Timer. I would have imagined that one would use the
-> Threading.Timer classes for general-purpose sleep/wake event handling
-> like this, however the constructor and methods you've used in your
-> example references the System.Windows.Forms.Timer class, which must
be
-> used in a Windows Forms application.
->
-> It is of course confusing to me why the framework has two different
-> classes with the same name.
->
-> The 'Elapsed' property also doesn't seem to be provided by either
-> class.
->
-> Basically I can't compile the example code you've supplied. :-)
->
-> Regards,
->
-> -bws

---
[This E-mail scanned for viruses by Declude Virus]

Reply to this message...
 
 
System.ComponentModel.Container
System.ServiceProcess.ServiceBase
System.Threading.Timer
System.Timers.ElapsedEventArgs
System.Timers.ElapsedEventHandler
System.Timers.Timer
System.Windows.Forms.Timer




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification