Link to home
Start Free TrialLog in
Avatar of systan
systanFlag for Philippines

asked on

simpliest example of windows service

hi
I'm willing to learn more, making apps via windows service
please attach a file for me to look at it.


thanks
Avatar of kaufmed
kaufmed
Flag of United States of America image

Here are two approaches. The cleanup in the thread-based example is a bit sloppy (I'm calling Abort(), which is not deemed good to do), but it should hopefully give you a starting point.

Timer Based
using System.ServiceProcess;

namespace WindowsService2
{
    public partial class Service1 : ServiceBase
    {
        private System.Timers.Timer _timer;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            this._timer = new System.Timers.Timer(2000);    // Interval set to 2 seconds
            this._timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);

            this._timer.Start();
        }

        protected override void OnStop()
        {
            this._timer.Stop();
        }

        void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            // Check some condition || do some operation || etc.            
        }
    }
}

Open in new window


Thread Based
using System.ServiceProcess;

namespace WindowsService2
{
    public partial class Service1 : ServiceBase
    {
        private System.Threading.Thread _thread;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            this._thread = new System.Threading.Thread(StartWorking);
            this._thread.Start();
        }

        protected override void OnStop()
        {
            this._thread.Abort();
        }

        private void StartWorking()
        {
            // Do some work
        }
    }
}

Open in new window

Avatar of systan

ASKER

Points increased for this connected question;

Do we really need a timer or a thread to do the servicing? <just yes or no>

If I choose the threaded approach, does that mean that it will continue to work within a loop until an abort command?  <just yes or no>

If I choose the timer approach, it  will NOT Lag a little on the computer?  <just yes or no>

If your answers defends on my service application,  then my service application is about detecting the path of the explorer window shell via Shell32 and SHDocVw.

So, its now really a yes or no answer,


thanks
Do we really need a timer or a thread to do the servicing?
Yes.

If I choose the threaded approach, does that mean that it will continue to work within a loop until an abort command?

No.

If I choose the timer approach, it  will NOT Lag a little on the computer?

There is a possibility due to the nature of timers, but I don't think you'd notice it.
Avatar of systan

ASKER

If I choose the threaded approach, does that mean that it will continue to work within a loop until an abort command?
>>No. ?
So, how can I do the threaded approach that continues a loop until I command it to abort?, just like the timer approached.

Points increased.


Thanks
So, how can I do the threaded approach that continues a loop until I command it to abort?, just like the timer approached.
I was trying to adhere to your "yes/no" stipulation  = )

Whether or not the thread continues will be dictated on how you construct the function that represents the work being done in the thread. If you were to do something like:

private void WorkerFunction()
{
    while (true)
    {
        // logic
    }
}

Open in new window


then it will continue to run until an exception occurs or Abort() is called on the thread (which in reality, causes an exception to be raised on the thread). Whether or not the thread continues to run until aborted will be dictated by your design.
Avatar of systan

ASKER

Do we really need a timer or a thread to do the servicing?
>>Yes ?
I'm sorry, I'm Lazy I really doubt about this.
Can you show a link that probes that making a windows service MUST have a thread or timer to enable to work permanently, except when someone stop or pause the service?

That will be my last.


Thanks

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of systan

ASKER

thank you
NP. Glad to help  : )