private static void RunStrategyTimer(object state) { Machines.ForEach(x => Console.WriteLine("ip: " + ((IPEndPoint)x.workSocket.RemoteEndPoint).Address.ToString() + " Mac Address: " + x.MacAddress)); StringBuilder record = new StringBuilder(); foreach (var s in Machines) { record.AppendLine(JsonSerializer.Serialize("Mac: " + s.MacAddress + " ip: " + ((IPEndPoint)s.workSocket.RemoteEndPoint).Address.ToString())); } Console.WriteLine("To record in file {0}", record.ToString()); var tt = DateTime.Now.ToString("HH:mm") + ":00"; StrategyExec strategyExec = new StrategyExec(); try { File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + "Connected machines details: " + record.ToString()); var ff = strategyExec.GetData(tt); Instructions inst = new Instructions(); if (ff.Count > 0) { //StrategyLogs stlogs = new StrategyLogs(); foreach (FinalResult f in ff) { try { File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + " Strategy Command : " + DateTime.Now.ToShortTimeString() + JsonSerializer.Serialize(f)); if (f.Instruction == "SystemOffStrategy") { int r = AsyncDesktopServer.SendToDesktop(f.Ccmac, f.Deskmac, "Shutdown", f.StrategyDescId, f.StrategyId); if (r == 0) { lock (Machines) { if (Machines.Count > 0) { if (Machines.Any(x => x.MacAddress == f.Ccmac)) { var t = Machines.Where(x => x.MacAddress == f.Ccmac).Select(x => x.workSocket).FirstOrDefault(); if (isClientConnected(t)) { // stlogs.SaveStrategyLogInfo(f.Instruction, f.StrategyId, f.StrategyDescId, "1", f.Ccmac); Send(t, inst.GetValues(f.Instruction)); } else { //stlogs.SaveStrategyLogInfo(f.Instruction, f.StrategyId, f.StrategyDescId, "0", f.Ccmac); } } } } } } else { lock (Machines) { if (Machines.Count > 0) { if (Machines.Any(x => x.MacAddress == f.Ccmac)) { var t = Machines.Where(x => x.MacAddress == f.Ccmac).Select(x => x.workSocket).FirstOrDefault(); if (isClientConnected(t)) { //stlogs.SaveStrategyLogInfo(f.Instruction, f.StrategyId, f.StrategyDescId, "1", f.Ccmac); Send(t, inst.GetValues(f.Instruction)); } else { //stlogs.SaveStrategyLogInfo(f.Instruction, f.StrategyId, f.StrategyDescId, "0", f.Ccmac); } } } } } } catch (Exception ex) { File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + "Error in RunStrategyTimer sending instruction to machine : " + ex.StackTrace); } } } } catch (Exception ex) { File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + "Error in RunStrategyTimer: " + ex.StackTrace); Console.WriteLine(ex.Message); } }