Click here to Skip to main content
16,021,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Geeks,

I want to send push notifications to android and IOS devices via windows service. Which has infinite loop, sending notifications one by one and it pauses for 60 seconds between two notifications. Notification to second device is depending upon previous device's response so, i cant use scheduler here.

Also, Is it good choice to use Thread.sleep(30000) to hold loop for 30 seconds?

Thanks in Advance.

What I have tried:

C#
protected override void OnStart(string[] args)
        {

            TimerStartApp = new System.Timers.Timer();
            this.TimerStartApp.Interval = Convert.ToDouble(60000);
            this.TimerStartApp.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerStartApp_Tick);
            TimerStartApp.AutoReset = false;
            TimerStartApp.Enabled = true;
            TimerStartApp.Start();
        }
..

private void TimerStartApp_Tick(object sender, ElapsedEventArgs e)
        {
                    
            this.TimerStartApp.Stop();
            this.TimerStartApp.Enabled = false;

            #region GCM Broker //Initializing gcmBroker

            #endregion

            #region APNS Broker //Initializing apnsBroker

            SendNotification();

            //WriteLog("TimerDelay_Tick() Executed");
            //this.TimerStartApp.Start();
        }

private void SendNotification()
        {

             while (true)
             {
               //Infinite loop
             }
        }
Posted
Updated 9-Apr-18 3:09am
v2

1 solution

The main problem of using an infinite loop is that your service can't be stopped. A proper implementation would do the work within a thread where the loop can be left upon a stop event. It is also necessary that the startup function of a Windows service returns in a timely fashion.

Use WaitHandle.WaitOne Method (Int32) (System.Threading)[^] to wait for a single event (the stop event) or WaitHandle.WaitAny Method (WaitHandle[], Int32) (System.Threading)[^] to wait for multiple events. Using the timeout parameter you can avoid using a timer and calling Sleep().

C#
private static ManualResetEvent stopEvent = new ManualResetEvent(false);
private static Thread theThread;

protected override void OnStart(string[] args)
{
    theThread = new Thread(ThreadFunc);
    theThread.IsBackground = true;
    theThread.Start();
}

protected override void OnStop()
{
     stopEvent.Set();
     if (!theThread.Join(10000))
         theThread.Abort();
}

private static void ThreadFunc()
{
    int timeOut = startDelay;
    while (!stopEvent.WaitOne(timeOut))
    {
        // Send notifications

        // Set timeout value
        timeOut = 30000;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900