Timer thread stops running

Discussion in 'ASP .Net' started by Todd, Apr 6, 2004.

  1. Todd

    Todd Guest

    I have an ASP.NET application and I would like to have some code run
    on the server automatically once a day at a specified time.

    I create a timer thread to call a simple callback in the
    Application_Start method. It seems to call the callback once or twice
    while the application starts up but appears to stop once the default
    page is displayed. Here is the code below (from Global.asax.cs):

    public static void ProcessAccounts (object state) {
    StreamWriter writer = new StreamWriter (@"c:\temp\testcallback.txt",
    true);
    writer.WriteLine (DateTime.Now.ToLongTimeString ());
    writer.Close ();
    }

    protected void Application_Start(Object sender, EventArgs e)
    {
    TimerCallback callback = new TimerCallback (ProcessAccounts);
    Timer timer = new Timer (callback, null, 0, 5000);
    }

    Any help would be much appreciated.

    Cheers,
    Todd.
    Todd, Apr 6, 2004
    #1
    1. Advertising

  2. the easiest way to do this is to drop a timer control onto your global.asax
    designer. Then customize it like you normally would using the interval
    settings. In the event handler you would just put your processaccounts code.
    Let .net do the management work for you instead of you doing it.

    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...
    http://tinyurl.com/27cok
    "Todd" <> wrote in message
    news:...
    > I have an ASP.NET application and I would like to have some code run
    > on the server automatically once a day at a specified time.
    >
    > I create a timer thread to call a simple callback in the
    > Application_Start method. It seems to call the callback once or twice
    > while the application starts up but appears to stop once the default
    > page is displayed. Here is the code below (from Global.asax.cs):
    >
    > public static void ProcessAccounts (object state) {
    > StreamWriter writer = new StreamWriter (@"c:\temp\testcallback.txt",
    > true);
    > writer.WriteLine (DateTime.Now.ToLongTimeString ());
    > writer.Close ();
    > }
    >
    > protected void Application_Start(Object sender, EventArgs e)
    > {
    > TimerCallback callback = new TimerCallback (ProcessAccounts);
    > Timer timer = new Timer (callback, null, 0, 5000);
    > }
    >
    > Any help would be much appreciated.
    >
    > Cheers,
    > Todd.
    Alvin Bruney [MVP], Apr 6, 2004
    #2
    1. Advertising

  3. Todd

    Todd Meynink Guest

    Alvin, thanks for your help.

    The drag and drop of the timer control from the Components gallery
    creates a server-based timer. My previous approach was creating a thread
    timer.

    The server-based timer appears to be basically working but I have two
    new problems with this approach.

    Firstly, it appears to fire the Elapsed event twice (one second apart)
    each time the specified interval elapses (in my test case 15 seconds).

    Second, I couldn't see how to easily set an initial elapse time followed
    by regular intervals of a standard elapse time (as you can with the
    System.Threading.Timer class).

    Regards,
    Todd


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Todd Meynink, Apr 6, 2004
    #3
  4. As i can see you have following
    protected void Application_Start(Object sender, EventArgs e)
    {
    TimerCallback callback = new TimerCallback (ProcessAccounts);
    Timer timer = new Timer (callback, null, 0, 5000);
    }

    what is the scope of the timer object?

    I hope you are getting my drift.

    In any normal language timer will disappear as soon as you left the Start.
    In .NET it's at random (whenever GS will eat you timer).
    the same goes to the callback.

    George.


    "Todd" <> wrote in message
    news:...
    > I have an ASP.NET application and I would like to have some code run
    > on the server automatically once a day at a specified time.
    >
    > I create a timer thread to call a simple callback in the
    > Application_Start method. It seems to call the callback once or twice
    > while the application starts up but appears to stop once the default
    > page is displayed. Here is the code below (from Global.asax.cs):
    >
    > public static void ProcessAccounts (object state) {
    > StreamWriter writer = new StreamWriter (@"c:\temp\testcallback.txt",
    > true);
    > writer.WriteLine (DateTime.Now.ToLongTimeString ());
    > writer.Close ();
    > }
    >
    > protected void Application_Start(Object sender, EventArgs e)
    > {
    > TimerCallback callback = new TimerCallback (ProcessAccounts);
    > Timer timer = new Timer (callback, null, 0, 5000);
    > }
    >
    > Any help would be much appreciated.
    >
    > Cheers,
    > Todd.
    George Ter-Saakov, Apr 6, 2004
    #4
  5. Todd

    Todd Guest

    George - that was it. Thank-you very much.

    Going to go and look for my brain now... :p

    "George Ter-Saakov" <> wrote in message news:<>...
    > As i can see you have following
    > protected void Application_Start(Object sender, EventArgs e)
    > {
    > TimerCallback callback = new TimerCallback (ProcessAccounts);
    > Timer timer = new Timer (callback, null, 0, 5000);
    > }
    >
    > what is the scope of the timer object?
    >
    > I hope you are getting my drift.
    >
    > In any normal language timer will disappear as soon as you left the Start.
    > In .NET it's at random (whenever GS will eat you timer).
    > the same goes to the callback.
    >
    > George.
    >
    >
    > "Todd" <> wrote in message
    > news:...
    > > I have an ASP.NET application and I would like to have some code run
    > > on the server automatically once a day at a specified time.
    > >
    > > I create a timer thread to call a simple callback in the
    > > Application_Start method. It seems to call the callback once or twice
    > > while the application starts up but appears to stop once the default
    > > page is displayed. Here is the code below (from Global.asax.cs):
    > >
    > > public static void ProcessAccounts (object state) {
    > > StreamWriter writer = new StreamWriter (@"c:\temp\testcallback.txt",
    > > true);
    > > writer.WriteLine (DateTime.Now.ToLongTimeString ());
    > > writer.Close ();
    > > }
    > >
    > > protected void Application_Start(Object sender, EventArgs e)
    > > {
    > > TimerCallback callback = new TimerCallback (ProcessAccounts);
    > > Timer timer = new Timer (callback, null, 0, 5000);
    > > }
    > >
    > > Any help would be much appreciated.
    > >
    > > Cheers,
    > > Todd.
    Todd, Apr 8, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelsang Wangchuk

    System.Timers.Timer vs. System.Threading.Timer

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    716
    Kelsang Wangchuk
    Jul 31, 2003
  2. =?Utf-8?B?bWFyZWFs?=

    Thread just stops running

    =?Utf-8?B?bWFyZWFs?=, Mar 8, 2005, in forum: ASP .Net
    Replies:
    9
    Views:
    559
    Kevin Spencer
    Mar 9, 2005
  3. Edmond wong
    Replies:
    4
    Views:
    525
    iksrazal
    Mar 29, 2005
  4. =?Utf-8?B?QW1yaXQgS29obGk=?=

    Everything stops when a Thread is running on the server

    =?Utf-8?B?QW1yaXQgS29obGk=?=, Jul 31, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    574
    =?Utf-8?B?QW1yaXQgS29obGk=?=
    Jul 31, 2007
  5. liu yang
    Replies:
    4
    Views:
    2,007
    Antoninus Twink
    Jul 28, 2008
Loading...

Share This Page