System.Threading.Timer and Dotnet 2.0

Discussion in 'ASP .Net' started by Stig Hausberg, May 12, 2005.

  1. Hi folks!
    I'm currently working in a prototype project as we are moving into dotnet
    and I've have stumbled upon an issue someone hopefully can help me with.

    The system we are building needs to run a specific part of code at a set of
    intervals, lets say once an hour. As the system will be installed on
    multiple servers we were hoping that we could avoid to use any sort of
    applications/windows services and build this into the asp.net solution we
    are creating.
    I've been looking into using System.Threading.Timer but it appears as the
    garbage collector collects the thread when the refernece dies if I use
    subsequent executions (It usually happens after 15-30 sec.). But if I only
    invoke the thread once in the future it appears to have a longer timespan
    (I've tried up to 6 minutes).

    As I'm relative fresh to the more advanced features of dotnet I would
    appriciate it if someone enlightened me about this subject or/and could
    point me in a direction(best practice) which could help me solve this
    problem.

    Regards
    Stig Hausberg
    Stig Hausberg, May 12, 2005
    #1
    1. Advertising

  2. Stig Hausberg

    Patrice Guest

    I'm not sure I really understand your description....

    "the garbage collector collects the thread when the refernece dies" : do you
    mean that the timer will not fire because the thread that create the timer
    is no more here te be warned...
    "But if I only invoke the thread once in the future" : or you already create
    another thread ????

    What does this code ? If this is some database handling it could be a a job
    scheduled on the DBMS side.
    If a service annoys you, it could be also just a regular EXE file that would
    be launched with the scheduling service.

    IMO stuffing this kind of things in an ASP.NET application is quite a hack
    (for example what if the ASP.NET application is recycled, you'll likely have
    to maintain the ASP.NET application always alive etc...)

    Patrice

    --

    "Stig Hausberg" <> a écrit dans le message de
    news:d5v19h$3tq$...
    > Hi folks!
    > I'm currently working in a prototype project as we are moving into dotnet
    > and I've have stumbled upon an issue someone hopefully can help me with.
    >
    > The system we are building needs to run a specific part of code at a set

    of
    > intervals, lets say once an hour. As the system will be installed on
    > multiple servers we were hoping that we could avoid to use any sort of
    > applications/windows services and build this into the asp.net solution we
    > are creating.
    > I've been looking into using System.Threading.Timer but it appears as the
    > garbage collector collects the thread when the refernece dies if I use
    > subsequent executions (It usually happens after 15-30 sec.). But if I only
    > invoke the thread once in the future it appears to have a longer timespan
    > (I've tried up to 6 minutes).
    >
    > As I'm relative fresh to the more advanced features of dotnet I would
    > appriciate it if someone enlightened me about this subject or/and could
    > point me in a direction(best practice) which could help me solve this
    > problem.
    >
    > Regards
    > Stig Hausberg
    >
    >
    Patrice, May 12, 2005
    #2
    1. Advertising

  3. The lifetime of a timer is limited to the lifetime of the class which
    instantiates it. If you're talking about a Page class, you're talking about
    a lifetime measured in milliseconds. A Page class exists for the length of
    time it takes to Respond to a single Request for that Page.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    What You Seek Is What You Get.

    "Stig Hausberg" <> wrote in message
    news:d5v19h$3tq$...
    > Hi folks!
    > I'm currently working in a prototype project as we are moving into dotnet
    > and I've have stumbled upon an issue someone hopefully can help me with.
    >
    > The system we are building needs to run a specific part of code at a set
    > of intervals, lets say once an hour. As the system will be installed on
    > multiple servers we were hoping that we could avoid to use any sort of
    > applications/windows services and build this into the asp.net solution we
    > are creating.
    > I've been looking into using System.Threading.Timer but it appears as the
    > garbage collector collects the thread when the refernece dies if I use
    > subsequent executions (It usually happens after 15-30 sec.). But if I only
    > invoke the thread once in the future it appears to have a longer timespan
    > (I've tried up to 6 minutes).
    >
    > As I'm relative fresh to the more advanced features of dotnet I would
    > appriciate it if someone enlightened me about this subject or/and could
    > point me in a direction(best practice) which could help me solve this
    > problem.
    >
    > Regards
    > Stig Hausberg
    >
    Kevin Spencer, May 12, 2005
    #3
  4. Stig Hausberg

    amar_kumar

    Joined:
    Aug 3, 2007
    Messages:
    1
    Threading.Timer problem

    I also face the same problem in windows service my threading.Timer fires once then then never fires for different intervals. Here is the code

    for (int iIndex = 0; iIndex < ds.Tables["Table1"].Rows.Count; iIndex++)
    try
    {
    lintInterval = int.Parse(ds.Tables["Table1"].Rows[iIndex]["SearchInterval"].ToString()) ;
    TimeSpan oSpan = TimeSpan.FromMinutes(lintInterval);
    System.Threading.Timer oTimer = new System.Threading.Timer(new System.Threading.TimerCallback(startCompare1),
    (object) iIndex, oSpan, TimeSpan.FromMilliseconds(-1));
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }
    amar_kumar, Aug 3, 2007
    #4
    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. Charles A. Lackman
    Replies:
    1
    Views:
    1,321
    smith
    Dec 8, 2004
  2. Kelsang Wangchuk

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

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    702
    Kelsang Wangchuk
    Jul 31, 2003
  3. SpamProof
    Replies:
    0
    Views:
    528
    SpamProof
    Oct 21, 2003
  4. Replies:
    1
    Views:
    1,572
    Steve C. Orr [MVP, MCSD]
    Feb 22, 2005
  5. Chuck
    Replies:
    3
    Views:
    491
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 8, 2007
Loading...

Share This Page