寻找最佳方式来查看我的任务何时完成C#

时间:2017-10-04 12:20:26

标签: c# task

所以,我要提取一个项目列表,并为每个项目创建一个对象实例,以便对该项目运行任务。所有对象都是相同的,它们每三秒钟根据收到的消息进行更新。这个更新不是一次全部发生,有时需要3.1秒,等等。这是我需要在XML中序列化的数据一旦存在,所以我正在寻找一种方法来查看它的完成时间。

我已经在.net 4.6中探索了任务,但它启动了一个任务,它报告完成,然后再次运行任务类会再次启动它,但在我的情况下,由于每个实例都停留,因此无法工作当有新消息进入时,它会自动启动。

报告它到达最后一行代码的最佳方法是什么,然后查看这些实例的列表并说明所有这些实例都显示为完成然后运行任务以序列化?

我已经包含了正在运行的实例的代码。

private void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            var eventArgs = new CallDataReceivedEventArgs();
            this.OnCallDataReceived(eventArgs);

            try
            {
                List<Tuple<String, TimeSpan>> availInItems = new List<Tuple<string, TimeSpan>>();
                List<Tuple<string, int, TimeSpan, string, string, string>> agentlist = new List<Tuple<string, int, TimeSpan, string, string, string>>();

                if (e == null)
                {
                    return;
                }

                List<TimeSpan> listOfTimeSpans = new List<TimeSpan>();
                if (e.CmsData != null)
                {
                    #region Gathering Agent Information
                    // Create a list of all timespans for all _agents in a queue using the property AgentTimeInState

                    foreach (var item in e.CmsData.Agents)
                    {
                        //AgentData = new ScoreBoardAgentDataModel(AgentName, AgentExtension, AgentTimeInState, AgentAuxReason, AgentId, AgentAdcState);

                        _agentData.AgentName = item.AgName;

                        _agentData.AgentExtension = item.Extension;

                        _agentData.AgentAuxReason = item.AuxReasonDescription;

                        _agentData.AgentId = item.LoginId;

                        _agentData.AgentAcdState = item.WorkModeDirectionDescription;

                        _agentData.AgentTimeInState = DateTime.Now - item.DateTimeUpdated;

                        _agentData.TimeSubmitted = DateTime.Now;

                        agentlist.Add(Tuple.Create(_agentData.AgentName, _agentData.AgentExtension, _agentData.AgentTimeInState, _agentData.AgentId, _agentData.AgentAcdState, _agentData.AgentAuxReason));

                        if (_agentData.AgentAcdState == "AVAIL")
                        {
                            listOfTimeSpans.Add(_agentData.AgentTimeInState);

                            availInItems.Add(Tuple.Create(_agentData.AgentName, _agentData.AgentTimeInState));
                        }

                        availInItems.Sort((t1, t2) => t1.Item2.CompareTo(t2.Item2));
                    }

                    var availInAgents =
                        agentlist
                            .Where(ag => ag.Item5 == "AVAIL")
                            .ToList();

                    availInAgents.Sort((t1, t2) =>
                        t1.Item3.CompareTo(t2.Item3));

                    var max3 = availInAgents.Skip(availInAgents.Count - 3);

                    max3.Reverse();

                    _agents.AgentsOnBreak = 0;
                    foreach (var agent in agentlist)
                    {
                        if (!string.IsNullOrEmpty(agent.Item6) && agent.Item6.StartsWith("Break"))
                        {
                            _agents.AgentsOnBreak++;
                        }
                    }

                    _agents.AgentsOnLunch = 0;
                    foreach (var agent in agentlist)
                    {
                        //If the current agent's aux reason is Lunch
                        if (!string.IsNullOrEmpty(agent.Item6) && agent.Item6.StartsWith("Lunch"))
                        {
                            //add one to agentsonlunch
                            _agents.AgentsOnLunch++;
                        }
                    }

                    _agents.NextInLine = string.Empty;
                    foreach (var agent in max3.Reverse())
                    {
                        //assign agent to NextInLine and start a new line
                        _agents.NextInLine += agent.Item1 + Environment.NewLine;
                        //reverse NextInLine
                        _agents.NextInLine.Reverse();
                    }

                    _agents.TimeSubmitted = DateTime.Now;

                    #endregion

                    #region Gathering Skill Information

                    _skillData.OldestCall = e.CmsData.Skill.OldestCall;
                    _skillData.AgentsStaffed = e.CmsData.Skill.AgentsStaffed;
                    _skillData.AgentsAuxed = e.CmsData.Skill.AgentsInAux;
                    _skillData.AgentsAvailable = e.CmsData.Skill.AgentsAvailable;
                    _skillData.AgentsOnCalls = e.CmsData.Skill.AgentsOnAcdCall;
                    _skillData.CallsWaitingInQueue = e.CmsData.Skill.InQueueInRing;
                    _skillData.Asa = e.CmsData.Skill.AnswerTimePerAcdCall;
                    _skillData.TimeSubmitted = DateTime.Now;
                    _skillData.EstimatedHoldTimeLow = e.CmsData.Skill.ExpectedWaitTimeLow;
                    _skillData.EstimatedHoldTimeMedium = e.CmsData.Skill.ExpectedWaitTimeMedium;
                    _skillData.EstimatedHoldTimeHigh = e.CmsData.Skill.ExpectedWaitTimeHigh;     
                    #endregion

                }
            }
            catch (Exception ex)
            {
                _logger.Info(ex.Message, ex);
            }
        }

1 个答案:

答案 0 :(得分:3)

通过任务,您可以同时启动多个任务并等待它们全部完成:

var taskList = new List<Task>();

foreach (var thingToDo in work)
{
   taskList.Add(thingToDo.StartTask());
}

Task.WaitAll(taskList.ToArray());

通过这种方式,您可以并行运行所有内容,并且不会在最后一行之后完成所有操作。

修改评论

您可以将您的工作嵌入到以下任务中:

public async Task DoWork() 
{
    var taskList = new List<Task>();

    foreach (var thingToDo in work)
    {
       taskList.Add(thingToDo.StartTask());
    }
    await Task.WhenAll(taskList.ToArray());
}