Why a static variable dies in Asp.Net

We know that the static variable is live as long as the application does not .

For example, we can count the number of visitors with one variable static int.

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

If the above sentences are correct, we can determine a static Timer, and we expect the fire event to be Elapsedforever.

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, within a few minutes, the increase Lable1.Textin a millisecond, but when 15 minutes have passed, this table only shows 0.

Why and what can I do for the perpetual timer ?

+4
1

. , , "reset", - .

aspx, asp.net .

ASP.NET

, , , , asp.net

+1

Source: https://habr.com/ru/post/1608481/


All Articles