Click here to Skip to main content
11,493,233 members (62,491 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ADO.NET
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
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 29-Jan-13 6:08am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
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();
  }
}
  Permalink  
v2
Comments
Atul Rokade at 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 at 29-Jan-13 12:07pm
   
why you write private DoProcess()
{
timer.Start();
}
this line im not getting
Atul Rokade at 29-Jan-13 12:12pm
   
sorry bro but timer not executed at define time
jibesh at 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 at 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
jibesh at 29-Jan-13 13:30pm
   
so the timer has to be keep running so that it can send email next week and so on right. if you stop the timer once it send emailattachment means you end the timer and it wont call this method until you start it again. is that what you are expecting?
Atul Rokade at 29-Jan-13 12:55pm
   
then where i can define timer.start() method?
Atul Rokade at 29-Jan-13 13:01pm
   
jibesh bro i do whatever u said but now timer not executed here im paste code please checkout
in constructor i write
timer = new System.Timers.Timer();

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

and timer_elapsed event i write


if ((DateTime.Now.DayOfWeek == DayOfWeek.Tuesday) && (DateTime.Now.Hour ==23) && (DateTime.Now.Minute ==27))
{


emailattachment();



}
timer.Stop();
jibesh at 29-Jan-13 13:16pm
   
timer is execute only once and it stops at timer.Stop method.

Please tell me when you want to call this time. is there condition to send the emailAttachment other that the checking you added inside the elapsed method?
Atul Rokade at 29-Jan-13 13:18pm
   
ya sir have if condition in timer_elapsed event
if ((DateTime.Now.DayOfWeek == DayOfWeek.Tuesday) && (DateTime.Now.Hour ==23) && (DateTime.Now.Minute ==27))
{


emailattachment();



} this one
jibesh at 29-Jan-13 13:26pm
   
so before you use time you must understand the concept of how the timer is working let me explain the line by line
1. timer = new System.Timers.Timer();
you define a timer object
2. timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
subscribing the Elapsed method. which means timer_Elapsed method will be called every Interval you set at Step3 and only when you start the timer as defined by Step4. also note that if you keep the timer as member variable you can start the timer when ever you want.
3. timer.Interval = 5000;
setting the interval means your Elapsed event at Step2 will be triggered every 5 sec
4. timer.Start();
when you call the Start method timer starts ticking next moment and it starts firing the Elapsed event based on the interval set, and it keep ticking/firing Elapsed event for the interval you set at Step3 untill you execute the timer.Stop method.

so when you call the Stop method Elapsed event will not called until you start again.

start debug your application and put a break point inside the timer_Elapsed method to see what is happening.
Atul Rokade at 29-Jan-13 13:34pm
   
jibesh sir i put break point in timer_elapsed method but nothing happen no error come ,no timer execute :(
jibesh at 29-Jan-13 13:35pm
   
was it a Windows form application? also can you delete the timer.Stop method for the time being.
Atul Rokade at 29-Jan-13 13:38pm
   
yes sir it is windows application n no im not delete timer.stop method
jibesh at 29-Jan-13 13:39pm
   
Ok. Just follow me Ok.
put these lins in your constructor.
timer = new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 5000;
Atul Rokade at 29-Jan-13 13:40pm
   
iam put here im update code
globally i define
private System.Timers.Timer timer = null;

in constructor i write
timer = new System.Timers.Timer();

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

and in timer_elapsed


if ((DateTime.Now.DayOfWeek == DayOfWeek.Tuesday) && (DateTime.Now.Hour ==00) && (DateTime.Now.Minute ==03))
{


emailattachment();



}
timer.Stop();
jibesh at 29-Jan-13 13:42pm
   
please delete the lines timer.Start and timer.Stop and Place two buttons (one with caption Start and another Stop)in your form this to make you understand how timer works. please follow me.

On Button Start Click - call the line timer.Start and
on button Stop click - call the line timer.Stop
Atul Rokade at 29-Jan-13 13:45pm
   
ok..now?
jibesh at 29-Jan-13 13:47pm
   
Put a break point inside your timer_elapsed and start the application.

Press the Start button and see this break point is hit or not.
Atul Rokade at 29-Jan-13 13:51pm
   
jibesh sir this time timer run only one time and timer successfully executed at given time but..this methods y not execute in form load n timer_elapsed
jibesh at 29-Jan-13 13:56pm
   
You are wrong again. it's not only one time. wait for 5 sec and see it again it will be keep ticking for every 5 seconds because you configured the interval as 5000. timer will be keep running until you execute the timer.Stop method.

Ok now coming back to your question 'this methods y not execute in form load n timer_elapsed'

place the timer.Start method inside the Form_Load method and see what happens now. I hope there is no other change in your code.
Atul Rokade at 29-Jan-13 13:59pm
   
n what about timer.stop() method can i write in timer_elapsed event?
jibesh at 29-Jan-13 14:05pm
   
it depends on your requirement.

if you want the timer elapsed to execute only once until it meets the condition move the timer.Stop inside the if condition provided inside the method timer_elapsed.
but if you want your application to work like a watch dog ( watch for every Tuesday in a week) and send email then you should not stop the timer let it run and add a bool variable to check whether this method is called for the day or not.
Atul Rokade at 29-Jan-13 14:08pm
   
jibesh sir same problem it execute many times i write timer.stop() method in if condition then also this problem come
jibesh at 29-Jan-13 14:12pm
   
Atul, You still doesnt understand the basics of the timer. unless it runs many time how will you detect the current time and day?

it will execute continuously and we perform the operation only when the condition is met. if it executes only once why do you need a timer at all? cant you use simple function?

now tell me why you choose a timer here? and what makes you to choose timer here? any answer for this?
Atul Rokade at 29-Jan-13 14:16pm
   
jibesh sir i thought timer can run automatically without user clicking any button if condition met timer.. he will do his job user cannot do anything in this condition . it my concept and one of our member also suggest me use timer in your application that's why i used :)
jibesh at 29-Jan-13 14:26pm
   
hummm.. now i get the point i suggest you to do spend some time on understanding the operations when someone suggest, he will be under the impression you know the rest.

'he will do his job user cannot do anything in this condition'
this is not completely true. timer is just like our clock. our clock is ticking every second and it runs forever so we know when it completes hr, minute, day, week etc...

likewise when you call the timer.Start method - it starts ticking..
Please read this article at MSDN this will give the picture of how Timer is working.

It's still Ok that your timer_elapsed will called multiple time , by adjusting the Interval you can reduce the intensity since your checking doesnt count the seconds you set the timer to tick every one minute i.e(1000 * 60 = 60000 secs)
Atul Rokade at 29-Jan-13 14:31pm
   
ok jibesh sir now my concept about timer is very clear thank you very very much for your valuable support :)
jibesh at 29-Jan-13 14:31pm
   
Glad that helps you. Good Luck.
Atul Rokade at 29-Jan-13 14:33pm
   
thank you very much god bless you :)
Atul Rokade at 29-Jan-13 14:34pm
   
thank you and god bless you :)
jibesh at 29-Jan-13 14:54pm
   
If your problem is solve please mark the solution as solved so that it help others its already answered and no need of answering it again. thanks

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 419
1 Maciej Los 286
2 F-ES Sitecore 224
3 KarstenK 210
4 CPallini 208
0 Sergey Alexandrovich Kryukov 10,132
1 OriginalGriff 8,630
2 Sascha Lefèvre 3,624
3 Maciej Los 3,392
4 Richard Deeming 2,500


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100