public static OBDJobService Instance() { //Debug.Log(instance); if (instance == null) { instance = new OBDJobService(); } return(instance); }
public void ExecuteCommandJob(String commandName) { OBDCommand command = new OBDCommand(commandName); try { if (!runningJobsList.Any(c => (c.getCommand()._obdCommandName == commandName))) { CommandJob obdCommandJob = new CommandJob(); CancellationTokenSource cancelToken = new CancellationTokenSource(); obdCommandJob.setCancellationToken(cancelToken); obdCommandJob._command = command; obdCommandJob._commandTask = Task.Run(async() => // <- marked async { while (!cancelToken.Token.IsCancellationRequested) { if (OBDJobService.Instance().GetVehicleConnectionStatus()) { messageRequestQueue.Add(command); Debug.Log("new command added, commandqueue count " + messageRequestQueue.Count); } await Task.Delay(200); // <- await with cancellation } }); Debug.Log("NEW TASK ADDED FOR : " + obdCommandJob.getCommand()._obdCommandName); runningJobsList.Add(obdCommandJob); obdCommandJob._commandTask.Start(); //obdCommandJob.setState(CommandJob.ObdCommandJobState.RUNNING); } else if (runningJobsList.Any(c => (c.getCommand()._obdCommandName == commandName) && (c._commandTask.Status != TaskStatus.Running))) { CommandJob obdCommandJob = runningJobsList.Find(c => (c.getCommand()._obdCommandName == command._obdCommandName)); Debug.Log("RESTARTING TASK ADDED FOR : " + command._obdCommandName); obdCommandJob._commandTask.Start(); } } catch { } finally { } }
static void Main(string[] args) { OBDJobService.Instance().ExecuteCommandJob("RPM"); }