private void StartRequestQueueProcessor() { Task requestProcessorTask = Task.Run(async() => { try { while (!m_cancelTokenSrc.Token.IsCancellationRequested) { if ((messageRequestQueue.Count > 0)) { OBDCommand command = messageRequestQueue.Take(); if (Instance().GetVehicleConnectionStatus()) { //Debug.Log(vehicle.GetCurrentData(command._obdCommandName)); command._responseValue = ObdAdapter.GetCurrentData(command._obdCommandName).ToString(); //Debug.Log("REQUEST QUEUE PROCESSING " + messageRequestQueue.Count); } else { command._responseValue = "NO DATA"; } messageResponseQueue.Add(command); } await Task.Delay(200); } } catch (Exception e) { Debug.Log("Exception: {0}" + e); } }); }
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 { } }