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
LVL 14
systanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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

0
systanAuthor Commented:
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
0
käµfm³d 👽Commented:
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.
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

systanAuthor Commented:
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
0
käµfm³d 👽Commented:
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.
0
systanAuthor Commented:
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

0
käµfm³d 👽Commented:
I'm sorry, I'm Lazy I really doubt about this.

From http://msdn.microsoft.com/en-us/library/aa984464%28v=vs.71%29.aspx:

Note   A service application is designed to be long running. As such, it usually polls or monitors something in the system. The monitoring is set up in the OnStart method. However, OnStart does not actually do the monitoring. The OnStart method must return to the operating system once the service's operation has begun. It must not loop forever or block. To set up a simple polling mechanism, you can use the System.Timers.Timer component. In the OnStart method, you would set parameters on the component, and then you would set the Timer.Enabled property to true. The timer would then raise events in your code periodically, at which time your service could do its monitoring.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
systanAuthor Commented:
thank you
0
käµfm³d 👽Commented:
NP. Glad to help  : )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.