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?
 
gregoryyoungConnect With a Mentor Commented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.