Creating a c# windows service to poll a database

后端 未结 2 987
栀梦
栀梦 2021-01-31 05:15

I am wanting to write a service that polls a database and performs an operation depending on the data being brought back.

I am not sure what is the best way of doing thi

2条回答
  •  被撕碎了的回忆
    2021-01-31 06:01

    You have a few options. To start with what could be essentially the easiest option, you could decide to create your app as a console application and run the executable as a task in the Windows Task Scheduler. All you would need to do is assign your executable as the program to start in the task and have the task scheduler handle the timing interval for you. This is probably the preferred way if you don't care about state and will prevent you from having to worry about creating and managing a windows service if you don't really need to. See the following link for how to use the scheduler.

    Windows Task Scheduler

    The next way you could do this would be to create a windows service and in that service use a timer, specifically System.Timers.Timer. Essentially you would set the timer interval to the amount of time you would like to have pass before you run your process. Then you would sign up for the timers tick event which would fire every time that interval occurred. In this event you would essentially have the process you would like to run; this could kick off addition threads if you would like. Then after that initial setup you would just call the timers Start() function or set the Enabled property to True to start the timer. A good example of what this would look like can be found in the example on MSDN page describing the object. There are plenty of tutorials out there that show how to set up a windows service so I won't bother with going into that specifically.

    MSDN: System.Timers.Timer

    Finally and more complex would be to set up a windows service that listens for a SqlDependency. This technique is useful if things can occur in the database outside your application yet you need to be made aware of it in your application or some other service. The following link has a good tutorial on how to set up a SqlDependency in an application.

    Using SqlDependency To Monitor SQL Database Changes


    Two things I would like to point out from your original post that are not specific to the question you had.

    1. If you are writing a true windows service you don't want the service to stop. The service should be running constantly and if an exception does occur it should be handled appropriately and not stop the service.

    2. A cancellation token doesn't have to throw an exception; simply not calling ThrowIfCancellationRequested() will cause the exception not to be thrown or if this is a CancellationTokenSource set the argument to false on the Cancel method then subsequently check the token to see if cancellation is requested in your threads and return out of the thread gracefully if so.

    For example:

        CancellationTokenSource cts = new CancellationTokenSource();
        ParallelOptions options = new ParallelOptions
        {
            CancellationToken = cts.Token
        };
        Parallel.ForEach(data, options, i =>
        {
            try
            {
                if (cts.IsCancellationRequested) return;
    
                //do stuff
    
            }
            catch (Exception ex)
            {
                cts.Cancel(false);
            }
        });  
    


提交回复
热议问题