using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Threading; namespace Iob.Core { /// /// Gestore generale dei threads /// public static class ThreadsHandler { private static List ThreadsFunctionsList = new List { #if false ThreadsFunctions.ManageWatchdog, ThreadsFunctions.ReadAdapter, ThreadsFunctions.SendData, ThreadsFunctions.ExecuteMapoRequests, #endif ThreadsFunctions.StatThread }; #if false private static Action ThreadSetupCmsConnect = ThreadsFunctions.SetupCmsConnect; #endif private volatile static List RunningThreadsList = new List(); public static Thread StartClient; internal volatile static Dictionary RunningThreadStatus = new Dictionary(); public static void Start() { #if false ThreadsFunctions.RestoreConnection(); if (Config.ServerConfig.ServerStartupConfig.CmsConnectReady) { Thread t = new Thread(() => ThreadSetupCmsConnect()); t.Start(); Thread.Sleep(30); } #endif } public static void StartWorkers() { lock (RunningThreadStatus) RunningThreadStatus.Clear(); SteamWare.IO.Redis myRedis = new SteamWare.IO.Redis(); string myKey = myRedis.redHash("StatThread"); // For each function run in the list a thread ThreadsFunctionsList.ForEach(threadFunction => { // Run new Thread in the list Thread t = new Thread(() => threadFunction() ); t.Start(); // aggiungo pausa x evitare sovrapposizioni chiamate Thread.Sleep(50); // Add thread to running threads list lock (RunningThreadsList) RunningThreadsList.Add(t); RunningThreadStatus.Add(threadFunction.Method.Name, "---"); }); #if false MessageServices.Current.Publish(SEND_THREADS_STATUS, null, RunningThreadStatus); #endif myRedis.setRSV(myKey, JsonConvert.SerializeObject(ThreadsHandler.RunningThreadStatus)); } public static void Stop() { // Stop each thread lock (RunningThreadsList) RunningThreadsList.ForEach(thread => { thread.Abort(); }); // Remove threads from running status lock (RunningThreadsList) RunningThreadsList.Clear(); lock (RunningThreadStatus) { RunningThreadStatus.Clear(); RunningThreadStatus.Add("TryNcConnection", "---"); } #if false MessageServices.Current.Publish(SEND_THREADS_STATUS, null, RunningThreadStatus); #endif } public static void Close() { //Abort Nc Read Threads RunningThreadsList.ForEach(thread => { thread.Abort(); }); if (ThreadsHandler.StartClient != null) ThreadsHandler.StartClient.Abort(); #if false //Abort Connect Thread ThreadsFunctions.AbortNcConnection(); #endif } public static void StartClientFromUI() { #if false ThreadsFunctions.StartCMSClient(); #endif } } }