web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Windows Service idle

(0) ShareShare
ReportReport
Posted on by 155

I have a windows service that communicates with a DB residing on a different server. The service is installed and always running. The service will look out if there any new records in the DB Table and get 1000 records at a time via stored proc and process records (updates/creates in the CRM 2016). Logic is working perfectly alright but the problem is with service going to idle state (this service shows as running in windows services  but won't execute the method 'ProcessNewOtranRecords' that calls stored proc) after few hours. When the service is restarted it again works as expected for few more hours.

Please suggest me if there is any good approach to keep service active all the time.

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Security.Permissions; 
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using CRM.Objects.BusinessLogic; 

namespace CRM
{
    partial class CrmProcessOtran : ServiceBase
    {
        OtranBL _otranBL = new OtranBL();
        private System.Timers.Timer mainTimer;
        int eventID = 0;

        public CrmProcessOtran()
        {
            InitializeComponent();

        }

        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("Service Start");
            
            mainTimer = new System.Timers.Timer(30000); // 30 seconds
            mainTimer.Elapsed += PerformOtranOperations;
            mainTimer.AutoReset = false;
            mainTimer.Start(); 
        }


        protected override void OnStop()
        {
            eventLog1.WriteEntry("Service Stopped");
            
            mainTimer.Stop();
            mainTimer.Dispose();
            mainTimer = null; 
        }

        public void PerformOtranOperations(object sender, EventArgs e)
        {
            eventID++; 
            eventLog1.WriteEntry(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss") + " - checking for new otran records", EventLogEntryType.Information, eventID);
            // Check for new otran records 
            int otranRecords = _otranBL.GetOtranRecordCount(eventLog1);
            if (otranRecords == 0)
            {
                eventLog1.WriteEntry("0 new otran records", EventLogEntryType.Information, eventID);
                return;
            }

            eventLog1.WriteEntry(otranRecords.ToString("N0") + " new otran records found with proc_status = 0", EventLogEntryType.Information, eventID);
            // Process new records 
            eventLog1.WriteEntry(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss") + " - Begin processing new otran records", EventLogEntryType.Information, eventID);
            int processedrecords = _otranBL.ProcessNewOtranRecords(eventLog1);
            eventLog1.WriteEntry(processedrecords.ToString() + " processed records", EventLogEntryType.Information, eventID);

            mainTimer.Start();
        }
    }
}


*This post is locked for comments

I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans