• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

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!
0
John500
Asked:
John500
1 Solution
 
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
 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now