public void Start() { var files = Directory.GetFiles(Environment.CurrentDirectory).Where(r => r.EndsWith("dll")).ToList(); foreach (var file in files) { var assem = Assembly.LoadFile(file); var types = assem.GetExportedTypes().Where(r => r.IsClass && r.GetInterfaces().Any(i => i.Equals(typeof(Cpp.CppInterface))) || r.GetInterfaces().Any(i => i.Equals(typeof(CS.CSInterface))) ).ToList(); foreach (var type in types) { var process = new ProcessObject(); process.CancellationToken = new CancellationTokenSource(); process.DllProcess = Activator.CreateInstance(type); if (process.DllProcess is CS.CSInterface) { var p = process.DllProcess as CS.CSInterface; p.OnNotify += OnNotify; } else if (process.DllProcess is Cpp.CppInterface) { var p = process.DllProcess as Cpp.CppInterface; p.OnNotify += OnNotify; } _processes.TryAdd(type.Name, process); } } foreach (var process in _processes) { ThreadPool.QueueUserWorkItem(ProcessStart, process.Value); } }
private void ProcessStart(object process) { ProcessObject state = process as ProcessObject; var stopMethod = state.DllProcess.GetType().GetMethod("Stop", BindingFlags.Instance | BindingFlags.Public); while (!state.CancellationToken.IsCancellationRequested) { try { if (state.Started) { continue; } var initMethod = state.DllProcess.GetType().GetMethod("Init", BindingFlags.Instance | BindingFlags.Public); initMethod?.Invoke(state.DllProcess, null); var runMethod = state.DllProcess.GetType().GetMethod("Run", BindingFlags.Instance | BindingFlags.Public); runMethod?.Invoke(state.DllProcess, null); state.Started = true; } catch (Exception ex) { stopMethod?.Invoke(state.DllProcess, null); state.Started = false; Trace.WriteLine(ex.Message); } finally { Thread.Sleep(1000); } } if (state.DllProcess is CS.CSInterface) { var p = state.DllProcess as CS.CSInterface; p.OnNotify -= OnNotify; } else if (state.DllProcess is Cpp.CppInterface) { var p = state.DllProcess as Cpp.CppInterface; p.OnNotify -= OnNotify; } stopMethod? .Invoke(state.DllProcess, null); Console.WriteLine($"Thread Close"); }