CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2012
    Posts
    3

    How do I solve this problem without a continuous loop

    Hello everyone,
    I'm developing a project where I have a managing application with two tcplisteners, and i have 2 types of clients connecting to this application. The purpose is that the type1 client sends a task to the central application and then that application sends the message to the type2 client so it executes that task, this message is only sent if there are type2 clients available (not executing a task already), otherwise they stay in queue.
    I've thought of using a queue to achieve this, but the problem is, some tasks can't be started without the previous task being completed, also, the tasks and different priority levels.
    Maybe I'm not being clear what i mean is the following:
    task1 is sent with priority level 2.
    task2 is sent with priority level 2 and depends on task1 to be complete.
    task3 is sent with priority level 1.

    Imagining the task1 is still being executed on client1 and there is a client available to process tasks, how would I code it so the central application starts the task3 keeping the task2 in queue?
    I could keep a continuous loop reading all records from the task queue but obviously that would burn all the computer resources.
    Also keep in mind that the task queue can have periods without any records.

  2. #2
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: How do I solve this problem without a continuous loop

    I would create a List<classTask> for every task priority (if you know how many you will have) and have the TCP Listener fire an event when a type2 client is available. Then, when one is, take care of the of the first priority list available, until that list is empty, then move to the next list (priority). Something like
    Code:
    private void TCP_Type2ClientAvailableEventHandler(classOfType2 sender, EventArgs e)
    {
            if (PriorityOneList.Items.Count > 0)
         {
              ((classOfType2)sender).ProcessTask(PriorityOneList.Items(0);
              PriorityOneList.RemoveAt(0);
         }
         else if (PriorityTwoList.Items.count > 0)
         {
    
         }      
          ...etc
    }
    Since a List<> is ordered (as opposed to a Dictionary) you would be processing incoming tasks FIFO. If you don't know how many priority levels you will have, you could have a List of lists... but I'm not sure how that would work performance wise and all that =)
    Last edited by Nikel; July 26th, 2012 at 03:09 PM. Reason: Hit submit by error

  3. #3
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: How do I solve this problem without a continuous loop

    The tasks would naturally be added to those Lists<> by the Type1 clients. And you'd have to save the case when no tasks were pending and a type2 client comes available... store it somewhere else to keep track of what you have idle.

  4. #4
    Join Date
    Jul 2012
    Posts
    3

    Re: How do I solve this problem without a continuous loop

    That's an excellent idea! I guess i was stuck on the idea of a loop, and wasn't thinking "outside the box".

    I guess lists are the way to go, but can you make something clear for me? When i delete an item of a list does the next item gets the index of the deleted item?
    I mean, if i delete the item with index 0 will the item with index 1 get index 0?

  5. #5
    Join Date
    Dec 2009
    Posts
    145

    Re: How do I solve this problem without a continuous loop

    Yes, List will automatically relocates its items, which is faster than Array/ArrayList

  6. #6
    Join Date
    Jul 2012
    Posts
    3

    Re: How do I solve this problem without a continuous loop

    I will solve it like Nikel suggested.

    Thank you all so much for the help, it will definitely improve my project
    Last edited by miquelx; July 27th, 2012 at 08:23 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured