/// <summary> /// The client calls this method on the server to begin the request /// </summary> /// <param name="request"></param> /// <returns></returns> public Task SubmitTaskRequest(TaskRequest request) { string groupId = Context.ConnectionId; Queue.QueueBackgroundWorkItem(async token => { var rand = new Random(); var guid = Guid.NewGuid().ToString(); for (int delayLoop = 0; delayLoop <= 100; delayLoop += 10) { try { _logger.LogInformation($"Delay loop {delayLoop} for {groupId}"); await _hubContext.Clients.Group(groupId).ShowProgress(delayLoop); } catch (Exception ex) { _logger.LogError(ex, $"An error occurred sending progress. Error: {ex.Message}"); } await Task.Delay(TimeSpan.FromSeconds(rand.Next(1, 5)), token); } await _hubContext.Clients.Group(groupId).ReturnTaskResult(new TaskResult { Name = request.Name, TaskId = guid }); _logger.LogInformation($"Queued Background Task {guid} is complete."); }); return(Task.CompletedTask); }
/// <summary> /// The client calls this method on the server to begin the request /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task SubmitTaskRequest(TaskRequest request) { var rand = new Random(); var guid = Guid.NewGuid().ToString(); for (int delayLoop = 0; delayLoop <= 100; delayLoop += 10) { try { _logger.LogInformation($"Delay loop {delayLoop}"); await ShowProgress(delayLoop); } catch (Exception ex) { _logger.LogError(ex, $"An error occurred sending progress. Error: {ex.Message}"); } await Task.Delay(TimeSpan.FromSeconds(rand.Next(1, 5))); } await ReturnTaskResult(new TaskResult { Name = request.Name, TaskId = guid }); _logger.LogInformation( "LongRunning Task {Guid} is complete.", guid); }