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

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans