[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Processing task every 10 mins & an hour

Posted on 2008-02-02
16
Medium Priority
?
229 Views
Last Modified: 2013-12-29
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

0
Comment
Question by:ambuli
  • 6
  • 5
  • 5
16 Comments
 
LVL 10

Assisted Solution

by:Smart_Man
Smart_Man earned 400 total points
ID: 20804923
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
 

Author Comment

by:ambuli
ID: 20804974
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
 
LVL 8

Expert Comment

by:Yiogi
ID: 20804986
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 10

Expert Comment

by:Smart_Man
ID: 20804987
great. anyway is the loginc i have sent usefull for you ?

is that what you are looking for ?

waiting for yoru reply
0
 

Author Comment

by:ambuli
ID: 20805020
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
 
LVL 8

Expert Comment

by:Yiogi
ID: 20805027
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
 

Author Comment

by:ambuli
ID: 20805030
Should I have to have a separate thread for the hourly processing?
0
 

Author Comment

by:ambuli
ID: 20805040
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
 
LVL 8

Accepted Solution

by:
Yiogi earned 1600 total points
ID: 20805044
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
 
LVL 8

Expert Comment

by:Yiogi
ID: 20805050
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
 
LVL 10

Expert Comment

by:Smart_Man
ID: 20805075
:)))) 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
 

Author Comment

by:ambuli
ID: 20805076
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
 
LVL 10

Expert Comment

by:Smart_Man
ID: 20805094
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
 
LVL 8

Expert Comment

by:Yiogi
ID: 20805099
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
 
LVL 10

Expert Comment

by:Smart_Man
ID: 20805392
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
 
LVL 8

Expert Comment

by:Yiogi
ID: 20805417
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 4 hours left to enroll

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question