0

I have many backend console App, need to use a timer on it I use the following To initiate and maintain timer on console App in main method :

 System.Timers.Timer statustimer =
                    new System.Timers.Timer(
                        double.Parse(ConfigurationManager.AppSettings["NotificationSendPeriod"]));
                statustimer.Elapsed += ((sender, eventArgs) =>
                {
                    try
                    {
                        ProcessData();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);

                    }
                });
                statustimer.Enabled = true; Console.ReadLine();

and in ProcessData I initiate a new thread each time the method Call ( I control the number of parallel running by a counter From app.config

 public static async Task<bool> ProcessData()
    {
        try
        {
            new Thread(() =>
            {
                // Do work here 

            }).Start();



            return true;
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
            _logInstance?.Error(exception, $"{exception.Message}\n{exception.StackTrace}");
            return false;
        }
    }

I noticed by usign the code above after a while the console app close even though am using Console.Readline() , or hang up and nothing happen

any idea what's wrong in the above code and how to fix

AMH
  • 6,363
  • 27
  • 84
  • 135
  • I believe the `Timer.Elapsed` event is already running on a separate thread each time it is triggered. I don't believe you would need to create a new one. (https://stackoverflow.com/questions/1435876/do-c-sharp-timers-elapse-on-a-separate-thread) – EMUEVIL Jun 18 '18 at 21:23
  • I already call it once on main – AMH Jun 18 '18 at 21:25
  • I tried to run your code and it's works. Is there any error /Exception log? – Pavan Jun 19 '18 at 14:37

0 Answers0