Click here to Skip to main content
15,886,676 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
hi friends,
iam creating one application where i sent email file to client side using timer . timer work fine he doing perfect work that i want but problem is that it cannot stop it continuously executed i want timer executed only one time which is i define. iam try timer.stop() method but cant help me please give some suggestion or help im paste here my code

for form load
System.Timers.Timer timer = new System.Timers.Timer();
             timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
             timer.Interval = 5000;
             timer.Start();


n timer elapsed event i wrote
C#
private void timer_Elapsed(object source, ElapsedEventArgs e)
     {


         System.Timers.Timer timer = new System.Timers.Timer();
         
         if ((DateTime.Now.DayOfWeek == DayOfWeek.Monday) && (DateTime.Now.Hour ==19) && (DateTime.Now.Minute == 46))
         {
             

             emailattachment();



         }
         timer.Stop();
Posted

1 solution

Keep the timer object as member to the class
Create the instance of the timer either in your class constructor of form load event
Start the timer from the method you want to send the e-mail, when time timer_Elapsed event is ticked your emailAttachement method will be called

sample implementation is
C#
class Sample
{
  private Timer timer = null;
  
  public Sample
  {
    timer = new System.Timers.Timer();
    timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    timer.Interval = 5000;
  }
  
  private DoProcess()
  {
    timer.Start();
  }
   
  private void timer_Elapsed(object source, ElapsedEventArgs e)
  {
//double check the logic here your emailattachment method will be called only when the condition is met and it stop the timer means this method wont get executed again until timer is started again
   if ((DateTime.Now.DayOfWeek == DayOfWeek.Monday) && (DateTime.Now.Hour ==19) && (DateTime.Now.Minute == 46))
    {
        emailattachment();
    }
   timer.Stop();
  }
}
 
Share this answer
 
v2
Comments
Atul Rokade 29-Jan-13 12:02pm    
jibesh sir but when i use timer.start() method in timer_Elapsed event it give me Error The name 'timer' does not exist in the current context here is my timer_Elapsed event code


private void timer_Elapsed(object source, ElapsedEventArgs e)
{
timer.Start();
if ((DateTime.Now.DayOfWeek == DayOfWeek.Tuesday) && (DateTime.Now.Hour ==22) && (DateTime.Now.Minute ==28))
{


emailattachment();



}
timer.Stop();
//timer = null;
//timer.Interval = 1;
// timer.Stop();// stops the timer
//timer.Dispose();
//timer.Close();
}

and in constructor i create timer object
Atul Rokade 29-Jan-13 12:07pm    
why you write private DoProcess()
{
timer.Start();
}
this line im not getting
Atul Rokade 29-Jan-13 12:12pm    
sorry bro but timer not executed at define time
Jibesh 29-Jan-13 12:50pm    
Do not start timer inside timer_elapsed method. Keep in mind that timer elapsed method will be called only during the timer tick and only when timer is started. So move those lines from timer elapsed method.

Now tell me when and how you want to execute the emailattachment method?
Atul Rokade 29-Jan-13 12:54pm    
jibesh sir i want suppose day=tuesday and time=23 and min=24 then emailattachment file execute email sent to client like that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900