Creating a contructor and destructor for a simple class that is called in a Windows Service

Hello,

I'm trying to put together a Windows Service that checks a registry key.  It will use an infinite FOR loop and sleep for two minutes.

The code already compiles and runs as a Windows Console application.  The program.cs of the Console program looks like this:

namespace RegistryCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            RegCheck RC = new RegCheck();

            RC.RunRegUpdate();

            return;
        }
    }
}

Considering this is the code for a service:

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
        }

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

What would I have to do to run a thread for this class?  This brings me to the constructor and destructor of my RegCheck class - I don't have any.  The class has one method RunRegUpdate() which does the check and ends - some 80 lines.

Do I need a constructor and destructor for this class?  How about for the Service, what's necessary to put this all together right?

Thanks!
John500Asked:
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.

gregoryyoungCommented:
Thread t = new Thread(Whatever.CheckRegistry);
t.IsBackground=true;
t.Start();


    class Whatever
    {
        static void CheckRegistry()
        {
            RegCheck RC = new RegCheck();

            RC.RunRegUpdate();

            return;
        }
    }



Of course you could also just use a timer ...

Cheers,

Greg
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
John500Author Commented:
Thanks for the help here.


Regarding the object 'Whatever' -  are you saying this would be the code for the service which looks like this:

namespace RegCheckService
{
             public partial class RegCheckService : ServiceBase
             {
                        InitializeComponent();
                        RegCheck RRC = new RegCheck();

                        RRC.RunRegUpdate();
                        return;
             }
          protected override void OnStart(string[] args)
        {
            Thread t = new Thread(RegCheckService .RegistryCheck);
            t.IsBackground=true;
            t.Start();
        }

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

You lost me on the placement of the code.  What is the 'IsBackground' for ?

Sure, I'd be interested in the timer.   If for no other reason, I still don't see where the Sleep() would go.

Thanks

           
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.