CodeLiturgy.Dashboard/BlueWest/Core/System/ThreadServer.cs

78 lines
2.1 KiB
C#
Raw Normal View History

2021-12-06 02:49:27 +03:00
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using BlueWest.Core.ComponentSystem;
using BlueWest.Tools;
namespace BlueWest.Core
{
public sealed class ThreadServer
{
public bool IsActive { get; private set; }
2022-08-19 19:47:35 +03:00
public EventManager EventManager => _eventManager;
2021-12-06 02:49:27 +03:00
private readonly EventManager _eventManager;
public ThreadServer(EventManager eventManager)
{
_eventManager = eventManager;
}
public void Init()
{
InstantiateControllers();
IsActive = true;
}
/*public static readonly Expression<Entity, EntityDto> MappingExpression = x => new EntityDto
{
Name = x.Name,
...
};*/
private static Type[]? GetTypesFromBehaviors()
{
return Assembly.GetAssembly(typeof(Artefact))
?.GetTypes()
.Where(aType => aType.IsClass
&& !aType.IsAbstract
&& aType.IsSubclassOf(typeof(Artefact)))
.ToArray();
}
private void InstantiateControllers()
{
var types = GetTypesFromBehaviors();
if (types == null)
{
return;
}
foreach (var t in types)
{
InstantiateBehavior(t);
}
}
private void InstantiateBehavior(Type type)
{
SpawnThread(() =>
{
object instantiatiable = Activator.CreateInstance(type);
Artefact behavior = instantiatiable as Artefact;
behavior.SetupEntity(_eventManager);
behavior.RunLoop();
});
}
public void SpawnThread( Action action, ThreadPriority threadPriority = ThreadPriority.BelowNormal)
{
var thread = new Thread(action.Invoke)
{
Priority = threadPriority
};
2022-08-19 19:47:35 +03:00
2021-12-06 02:49:27 +03:00
thread.Start();
}
}
}