In this example we create a controller that allows us to turn threads on and off.
Simply add a new empty "API Controller" to the Controllers folder.
Call it AdminController.cs
Paste the code below in to replace the content
usingMicrosoft.AspNetCore.Mvc;usingPerigee;usingPerigee.Helpers;// Visit https://docs.perigee.software to learn more// Visit https://perigee.software to purchase a license!namespacePerigeeIntroAPI.Controllers{ [Route("api/[controller]")] [ApiController]publicclassAdminController:ControllerBase {publicThreadRegistry reg { get; }publicAdminController(ThreadRegistry tr) {this.reg= tr; } [HttpGet]publicIActionResultTaskcontrol([FromQuery] string task, [FromQuery] bool start) { //Does this thread exist?if (reg.ContainsThreadByName(task)) {if (start) {reg.StartIfNotRunning(task); //Start if not startedreturnOk(); }else {reg.QueueStop(task,true); //Stop and do not auto restartreturnOk(); } }returnBadRequest(); } [Route("cron")] [HttpGet]publicIActionResultcron([FromQuery] string name, [FromQuery] string cron, [FromQuery] string result) { if (reg.ContainsThreadByName(name) || string.IsNullOrEmpty(cron) || string.IsNullOrEmpty(result)) return BadRequest();
reg.AddManagedThread(newManagedThread(name, (ct, l) => {l.LogInformation("result: {result}", result); }, cron,reg.CTS,reg.GetLogger<AdminController>())); return new JsonResult(new { Message = $"Will run a CRON thread named {name} {Cron.Parse(cron).ToString()}" });
} [Route("delete")] [HttpDelete]publicIActionResultremove([FromQuery] string name) {reg.RemoveThreadByName(name);returnOk(); } [Route("list")] [HttpGet]publicIActionResultlist() { return new JsonResult(new { Threads = reg.GetThreads().Select(f => new { Name = f.Name, Runtime = f.RunTime }).ToArray() });
} [Route("reboot")] [HttpGet]publicIActionResultreboot() {reg.RestartAllThreads();returnOk(); } [Route("exec")] [HttpGet]publicIActionResultexec([FromQuery] string name) {reg.ExecuteCRONNow(name);returnOk(); } }}
In this example we've created a simple endpoint we can call to turn anything on or off by simply checking the thread registry for the requested thread, then turning it on or off.
The port may be different - but you can see how easy it would be to turn something on or off!