Processing task every 10 mins & an hour

Hi Experts,

I have a queue of activities that I want to process.  First, I want to process them every 10 minutes, and if the task has been in the queue for more than an hour then I should process them every hour then on.  The queue data has the start time saved.  I am having difficulty coming up with a logic to handle this.  Any help would be appreciated.


public class QueueData()
{
     private Date m_startTime;
      ...
    
}
 
 
while(true) 
{
    // process the queue data every 10 minutes.  
    // if the queue member is in there for more than an hour 
    // then don't process them every 10 mins but process them every hour.
 
 
}

Open in new window

ambuliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Smart_ManCommented:
First, I want to process them every 10 minutes, and if the task has been in the queue for more than an hour then I should process them every hour then on.

what does mean (then on) ??

process them all every 10 hours ?
what about the process that have been executing for an hour ?it shoudl terminated immediatly ?


i dunno but here is a suggestion. to have a class (the moderator) that calls the other classes. it would be like the windows kernel or the *nix bash(shell) for example. anyways

the other classes have to send a start_time_stamp when starting. and send finished_time_stamp.

the admin class should have the every xx minute check_logic (without holding up the resources). if all the process have been finished then it is ok. if not then the admin should a terminate signal. and send a re-intiation signal again for teh process (if required) and set the xx to 1 hour or whatever.

waiting for your reply if that is working for you to start finding the pesudo-code, real code syntax for ya.
0
ambuliAuthor Commented:
Hi thank you.

I think I wasn't clear on my question.  I may need to give the whole picture.  What happens is that I send a web service request to another server and queries the server for status of my requests.  So, when I send the request I save the start time and other details in a queue.  Then I start to send status query to see if the server has processed my message.  If the server has not processed my message for an hour then it means it will not be necessary to poll him every 10 minutes.  So, it is okay to poll him every hour.
This is what I am trying to do.  Please let me know if it wasn't clear.


0
YiogiCommented:
Is this what you are looking for?
public class QueueData()
{
     private Date m_startTime;
     System.Threading.Thread m_ProcessingThread;
      ...
    
 
 
public void StartProcessingThread() {
     // Stop thread if already running and start a new one
     if (m_ProcessingThread != null)
        StopProcessingThread();
     // If not running just start new thread
     m_ProcessingThread = new System.Threading.Thread(new ThreadStart(ProcessInfinitely));
     m_ProcessingThread.Start();
} 
 
private void ProcessInfinitely() { 
	while(true) 
	{
	    // process the queue data every 10 minutes.  
	//Do your proceessing
	
	    // Check how long this has been running and sleep accordingly
	    if (((TimeSpan)(DateTime.Now - m_startTime)).TotalMinutes < 60) {
               // Sleep 1 second * 60 * 10, i.e. 10 min
               System.Threading.Thread.Sleep(60 * 1000 * 10);
            }
            else {
               // Sleep an hour
               System.Threading.Thread.Sleep(60 * 1000 * 60);
            }
            
	
	    // then don't process them every 10 mins but process them every hour.
 
	}
}
 
// You will of course set m_StartTime from somewhere else and have a thread to 
 
public void StopProcessingThread() {
     if (m_ProcessingThread != null)
       try {
          m_ProcessingThread.Abort();
       }
       catch (ThreadAbortException) {
       }
       catch {
       } 
} 
 
 
}

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Smart_ManCommented:
great. anyway is the loginc i have sent usefull for you ?

is that what you are looking for ?

waiting for yoru reply
0
ambuliAuthor Commented:
Hi Yiogi:

The problem with the above may that the thread is going to sleep an hour while it needs to process the other 10 mins one.

For example, if I have 3 requests in the queue and only one of them that is more than an hour.  Therefore, I have to process the other two every 10 mins while processing the third one every hour.
0
YiogiCommented:
Yes but I assume you have a separate QueueData class for each of the requests. Which would not be a problem at all. If you don't them make a subclass for each request. Then each request would have it's very own thread, and it will sleep accordingly.
0
ambuliAuthor Commented:
Should I have to have a separate thread for the hourly processing?
0
ambuliAuthor Commented:
Thanks, but If I have 100 requests in there then it means there would be 100 threads which may be costly.  The timing need not to be very accurate.  

0
YiogiCommented:
You don't but it all depends on what you want to do. If you have lets say a List or an array or a dictionary or some collection of data anyway, and you want to loop through it every 10 min in 1 single thread you can do it. Just make sure that you save an instance of a queue item class you make in that collection of yours and that in that queue item you save the starting time, and last processed time. This way you can check each and every item in your collection every 10 min, and process the ones you wish or simply skip the others until the hour has passed.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YiogiCommented:
oh and 100 threads aren't really costly :). I have programs running around 1000 threads and consume very little CPU on a good server.
0
Smart_ManCommented:
:)))) threading are great. but i guess if we have another logic to have the same class monitoring x(n) processes every y(m) minute that would be great. dunno

0
ambuliAuthor Commented:
The idea of saving last processing time is great.  I think that is what probably suits me well.  Could you please provide me a code snippet of how it can be done.

Thanks.
0
Smart_ManCommented:
ummm i am too lazy .... hehe .. sowwy

but i will have it done in few hours if that suits ya .. maybe asking yougi for a favour to use the design i suggest above and re-write those few lines :)))))))
0
YiogiCommented:
All you need is 2 variables in a different class where you save each queue item. A start and last processed time.

Now in your other class you'd have to be checking the times and setting them each time you process it like the code below. For checking how much time has passed see my original post.
public class QueueItem()
{
     private DateTime m_ItemStartTime;
     private DateTime m_ItemLastProcessedTime;
 
     // Your class constructor also sets the start time
     public QueueItem() {
        m_ItemStartTime = DateTime.Now;
     } 
 
     public DateTime SetItemLastProcessedTime {
        set {
           m_ItemLastProcessedTime = DateTime.Now;
        }
     }
 
     public DateTime ReadLastProcessedTime {
         return m_ItemLastProcessedTime;
     }
} 
 

Open in new window

0
Smart_ManCommented:
yiogi ..author .. just a suggestion.

what about to have an add method inthe class which allows adding more process later to be monitored instead of threads ?

what about to have an array inside the calss to have the variables like start time,end time , monitor duration , last time checked ,.... which can be used for the all process the class is monitoring ?

with some calculation teh same class can monitor multiple processes according to differnt start/end / duration time and keeping track of the last time checked too

:)
0
YiogiCommented:
hi smart_man.

With the second suggestion I gave, he doesn't need more than 1 thread. Your suggestion is good he'd have to have some structure which can keep start time and last modified time, along with the essential data he needs to keep for each item.

And for me structures and classes are the same thing. I usually prefer other classes to have everything more organized and greater flexibility in adding stuff later, thats why I proposed a separate class. I'm not a fan of structures unless I keep very basic data that I know will not need any additions in the future for sure.

In any way there are plenty of solutions to his question, he just needs to figure out which one is the most useful for him.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.