public async Task<WorkingAgent> GetCompatibleAgentFromList(RenderConfiguration configuration, List<Agent> list)
        {
            WorkingAgent compatible = null;            

            foreach (Agent info in list)
            {
                if (compatible == null)
                {
                    WorkingAgent worker = new WorkingAgent(configuration);

                    try
                    {
                        RenderMessage msg = await worker.Connect(info);

                        if (msg == RenderMessage.Supported)
                        {
                            compatible = worker;
                            this.currentCompatibleAgent = info;
                        }
                    }
                    catch (AgentNotReachableException)
                    {
                        if (this.OnAgentNotReachable != null)
                        {
                            this.OnAgentNotReachable(this, info);
                        }
                    }
                }
            }

            return compatible;
        }
 private void Worker_OnResultReceived(WorkingAgent agent, RCS_Render_Job_Result result)
 {
     if (this.workingAgents.Exists(x => x.Configuration.RenderJobID.Equals(result.ConcernedRenderJobID)))
     {
         this.HandleImageResult(result.ConcernedRenderJobID, result.Picture);
     }
 }
 private void Worker_OnAgentGotUnreachable(WorkingAgent agent)
 {
     if (this.OnAgentNotReachable != null)
     {
         this.OnAgentNotReachable(agent.Configuration.JobToDo, agent.Agent);
     }
 }
        private async void Worker_OnMessageReceived(WorkingAgent agent, RCS_Render_Job_Message message)
        {
            if (message.Message == RenderMessage.ProcessExited)
            {
                Job todo = agent.Configuration.JobToDo;

                if (this.OnAgentAborted != null)
                {
                    this.OnAgentAborted(todo, agent.Agent);
                }

                // Solution 1: F**k it. We just say that the resource is not available anymore.
                /*if (this.OnResourceNotAvailable != null)
                {
                    this.OnResourceNotAvailable(agent.Configuration.JobToDo.Resource);
                }*/

                // Solution 2: try it with another agent, except the current.
                this.CancelJob(todo);

                List<Agent> excluded = this.agentSelectors[todo].AvailableAgents.Where(x => !x.IP.Equals(agent.Agent.IP)).ToList();

                RenderConfiguration config = this.GetNewRenderConfiguration(todo);
                WorkingAgent worker = await this.agentSelectors[todo].GetCompatibleAgentFromList(config, excluded);

                this.HandleFoundWorkingAgent(todo, worker);
            }
        }
        private void HandleFoundWorkingAgent(Job job, WorkingAgent worker)
        {
            if (worker == null)
            {
                if (this.OnNoResourceCompatibleAgentFound != null)
                {
                    this.OnNoResourceCompatibleAgentFound(job);
                }
            }
            else
            {
                Debug.WriteLine("--> Found agent for job " + job.Resource.Name);

                if (this.currentlyTreatedJob != null && worker.Configuration.JobToDo.OrderingNumber == this.currentlyTreatedJob.OrderingNumber)
                {
                    worker.OnResultReceived += Worker_OnResultReceived;
                    worker.OnMessageReceived += Worker_OnMessageReceived;
                    worker.OnAgentGotUnreachable += Worker_OnAgentGotUnreachable;

                    this.workingAgents.Add(worker);
                }
                else
                {
                    worker.CancelRenderJob();
                }
            }
        }