Files
cms_thermo_active/Step.Tasks/ThreadsHandler.cs
T
2017-12-14 17:35:22 +01:00

83 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CMS_CORE;
using CMS_CORE.Demo;
using CMS_CORE.Fanuc;
using CMS_CORE.Osai;
using CMS_CORE.Siemens;
using static Step.Config.StartupConfig;
using static Step.Utils.Constants;
namespace Step.Core
{
public class ThreadsHandler
{
private List<Func<Nc, bool>> ThreadFunctionsList;
private List<Thread> RunningThreadsList;
public ThreadsHandler()
{
ThreadFunctionsList = new List<Func<Nc, bool>>
{
ThreadsFunctions.ReadAlarmsAsync,
ThreadsFunctions.ReadAlarmsAsync,
ThreadsFunctions.ReadAlarmsAsync,
ThreadsFunctions.ReadAlarmsAsync,
ThreadsFunctions.ReadAlarmsAsync
};
RunningThreadsList = new List<Thread>();
}
public void Start()
{
// Create numerical control instance
Nc numericalControl = SetNumericalControl();
// For each function run in the list a thread
ThreadFunctionsList.ForEach(threadFunction =>
{
// Run new Thread
Thread t = new Thread(() =>
threadFunction(numericalControl)
);
t.Start();
// Add thread to running threads list
RunningThreadsList.Add(t);
});
}
public void Stop()
{ // Stop each thread
RunningThreadsList.ForEach(thread =>
{
thread.Abort();
});
}
public Nc SetNumericalControl()
{
// Return new Numerical control instance choosed from the configuration
switch ((NC_VENDOR)NcConfig.NcVendor)
{
case NC_VENDOR.DEMO:
return new Nc_Demo(NcConfig.NcIpAddress, NcConfig.NcPort);
case NC_VENDOR.FANUC:
return new Nc_Fanuc(NcConfig.NcIpAddress, NcConfig.NcPort, 2000);
case NC_VENDOR.SIEMENS:
return new Nc_Siemens(2000);
case NC_VENDOR.OSAI:
return new Nc_Osai(NcConfig.NcIpAddress, NcConfig.NcPort, 2000);
}
return null;
}
}
}