Files
2021-12-01 18:05:50 +01:00

127 lines
3.6 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading;
namespace Iob.Core
{
/// <summary>
/// Gestore generale dei threads
/// </summary>
public static class ThreadsHandler
{
private static List<Action> ThreadsFunctionsList = new List<Action>
{
#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<Thread> RunningThreadsList = new List<Thread>();
public static Thread StartClient;
internal volatile static Dictionary<string, string> RunningThreadStatus = new Dictionary<string, string>();
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
}
}
}