Link to home
Start Free TrialLog in
Avatar of ambuli
ambuliFlag for United States of America

asked on

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

SOLUTION
Avatar of Smart_Man
Smart_Man
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ambuli

ASKER

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.


Avatar of Yiogi
Yiogi

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

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

is that what you are looking for ?

waiting for yoru reply
Avatar of ambuli

ASKER

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.
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.
Avatar of ambuli

ASKER

Should I have to have a separate thread for the hourly processing?
Avatar of ambuli

ASKER

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.  

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh and 100 threads aren't really costly :). I have programs running around 1000 threads and consume very little CPU on a good server.
:)))) 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

Avatar of ambuli

ASKER

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.
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 :)))))))
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

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

:)
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.