4

We know that static variable alive until the application alive.

For example, we can count the number of visitor with a single static int variable.

private static int numberOfVisitors = 0;
protected void Page_Load(object sender, EventArgs e)
{
    numberOfVisitors++;
}

If the above sentences are right, we can define a static Timer and we expect the Elapsed event fire forever.

So, I wrote this application:

public partial class WebForm1 : System.Web.UI.Page
{
    private static System.Timers.Timer timer = new System.Timers.Timer(100);
    private static int numberOfTicks = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = numberOfTicks.ToString();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        timer.Elapsed += timer_Elapsed;
        timer.Start();
    }

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        numberOfTicks++;

    }
}

After clicking Button1, for some minutes, the Lable1.Text increase per millisecond, but when 15 minutes have passed, this lable shows just 0.

Why and what can I do for the forever timer?

Mehdi Khademloo
  • 2,754
  • 2
  • 20
  • 40
  • 11
    "We know that static variable alive until the application alive." - No, static variables are retained as long as the AppDomain is. So if a new AppDomain is created, you'll get new variables... – Jon Skeet Sep 22 '15 at 12:33
  • 1
    When IIS recycles the app pool, static variables are reset – Yogi Sep 22 '15 at 12:37
  • 1
    @JonSkeet So what is the answer of: **what can I do for the forever timer?** – Mehdi Khademloo Sep 22 '15 at 12:38
  • 2
    Don't use ASP.NET for scheduled tasks - it's as simple as that. There's no "forever" in ASP.NET - you're in a hosted application that's being routinely recycled. If you really have no other option, it is possible to handle this, but I'd avoid it unless absolutely necessary. There's frameworks to help you if needed. Oh, and your code is not thread-safe. – Luaan Sep 22 '15 at 12:39
  • 1
    @MehdiKhademloo you could initialize your timer when the application starts, and persist the intermediate values somewhere else (DB?). But perhaps you should explain more what exactly you'd like to do. – Wiebe Tijsma Sep 22 '15 at 12:39
  • @Zidad the problem is not the value. The problem is the **Fire the timer elapsed event forever**. This example is just a example. Not more – Mehdi Khademloo Sep 22 '15 at 12:41
  • @MehdiKhademloo you can't, or at least you shouldn't. IIS can at any time decide to kill your process if it isn't used. Are you running on Azure? There's other options there... – Wiebe Tijsma Sep 22 '15 at 12:42
  • @Luaan The threading issure is not important now because this example is just an example and the full problem is another one. I think there is no other choice, I have to run a function at anytime of a day. So there is no option? – Mehdi Khademloo Sep 22 '15 at 12:46
  • 1
    Not from within ASP.NET. Unless there's a request, your application may not run at all. – Luaan Sep 22 '15 at 12:47
  • @MehdiKhademloo have a look at this post: http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx – Wiebe Tijsma Dec 14 '15 at 16:59

1 Answers1

1

Static variables persist for the life of the app domain. So the two things that will cause your static variables to 'reset' is an app domain restart or the use of a new class.

you are losing your static variable in your aspx page because asp.net decides to recompile your page in new class.

take a look at this link Understanding ASP.NET Dynamic Compilation

so, if you want to perform some task in a particular interval there are many solutions i think you should take a look at this Running task in background or this ones seems to be a better idea asp.net long running interval tasks

Community
  • 1
  • 1
SHM
  • 1,896
  • 19
  • 48