Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Threading a Timer Object

I'm writing a Windows service in C# that needs to periodically, e.g. every minute, check a POP3 mailbox and grab some emails.  The problem being, is that it may take longer than a minute for it to complete processing.  I've been doing some digging on the net and EE on how I might do this and have come up empty.  Here's what I need:

A timer object to fire every 1-5 minutes and start a new thread if needed to do the processing.
0
Phreak3eb
Asked:
Phreak3eb
2 Solutions
 
sabeeshCommented:
Instead of timer user thread. so you can manage each thread separately and you set time interval for thread also.
0
 
sabeeshCommented:
Instead of timer use thread. so you can manage each thread separately and you set time interval for thread also.
0
 
Phreak3ebAuthor Commented:
Could you give a little more detail?  I guess I don't understand what you mean...?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gregoryyoungCommented:
public void CheckMail() {
    while(true) {
    //actually check mail
    Thread.Sleep(minutestosleep * 60 * 60 * 1000);
    }
}


private Thread m_Thread;
private object m_LockObject;
public void StartThread() {
    lock(m_LockObject) {
    if(m_Thread != null) throw new Exception("Thread Already Started");
   m_Thread = new Thread(new ThreadStart(CheckMail));
   m_Thread.Start();
    }
}

public void StopThread() {
   lock(m_LockObject) {
       if(m_Thread == null) throw new Exception("No Thread to Stop");
       m_Thread.Abort(); //better would be to make the thread listen for being told to stop as abort is evil but for a quick eexample this should do
       m_Thread = null;
}
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
How about just Stop() the Timer when it fires...check for mail...then Start() the Timer up again?...

    Private tmr As New System.Timers.Timer

    Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmr.Elapsed
        tmr.Stop()

        ' check for mail...

        tmr.Start()
    End Sub
0
 
TaeSoftCommented:
Just use boolean flag to say the job isn't done yet and that it will be for the next 3 minutes.
0
 
Phreak3ebAuthor Commented:
Thanks for all of your help.  Both solutions from Idle Mind and gregoryyoung will work and give me a couple options to work with.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now