1

I build web-site and want to count clicks on some button. I create and try this class of counter:

public static class Counter
{
    public static int counter = 0;
}

Every time I click on the button the counter is increament (counter++) ans I see it in my site, But, if I close the chrome and enter again to my site the counter starts from zero.

How can I save the counter? "Static" dont need to do that?

Or K
  • 335
  • 1
  • 5
  • 12

4 Answers4

3

My bet is that it happens because the application space is flushed - it shouldn't reset just because you closed your browser window, thus abandoning the current session (if the session cookie isn't persistent, that is.)

Visual Studio may republish your files (if using a remote IIS) or just plainly restart a local IIS Express instance, depending on how you set your development environment; I do believe setting a specific content as Static would cause it to be available to all current sessions.

That said, you may want to keep it under the current session (using the Session object).

Optionally, if you want to persist information in between server restarts, you may try reading and writing to a local storage, such as a plaintext or XML file. You can find a very nice article about this on the following link:

http://www.codeproject.com/Articles/544839/Implement-ASP-NET-custom-XML-file-configuration

A more sophisticated version would use a local (or remote) database, for example.

Hope it works for you.

OnoSendai
  • 3,960
  • 2
  • 22
  • 46
  • 1
    +1 for offering fairly simple solution, but I think it's important to note that unmanaged access to a text file is not a production-worthy solution (it'd be the end of the interview if I were on the hiring side of the table), and not suitable for any application that has more than, say, 1 user. – 3Dave Jan 08 '14 at 21:56
  • That's a completely fair remark, @DavidLively, and you should keep that in mind Or K. – OnoSendai Jan 08 '14 at 21:58
2

static fields are unique per-process. Depending on your application pool configuration, you could have 2, 20 or 100 copies of that.

They're also not thread safe. There are very, very few instances (pun) where a static member is appropriate.

Just off the top of my head, a particular "instance" of a static will disappear when:

  1. The application pool is recycled. On IIS, this defaults to 20 minutes of inactivity.

  2. The application process exits (you may have multiple processes running within your app pool). This happens as part of (1), but will also happen if, say, you're using the Visual Studio debug web server (Cassini), have your project configured to launch the site for debugging, and close the browser that was launched initially. (This happens because VS considers closing the browser that it launched equivalent to saying "I'm done playing. Back to coding now," or hitting the stop button.)

  3. Another thread overwrites the value you've stored (google "race condition.")

You really, really should be storing this in a database. If you're building a website, you need a database anyway. ANYTHING related to application state should be stored in the database.

ALSO, this really, really shouldn't be happening server-side. Are you really performing a postback every time someone clicks anywhere on a page? If so, you have JavaScript in place to handle that, so just skip this insanity, have said script fire off an AJAX request, and have the target handler log it in the database.

3Dave
  • 28,657
  • 18
  • 88
  • 151
1

Looks like your using a web site so presuming ASP.net. There are a number of ways to store the information. Database could be one or a persistent cookie could be the way to do it. See this article on how to create cookies: How do I set/unset cookie with jQuery?

Community
  • 1
  • 1
Rob
  • 6,819
  • 17
  • 71
  • 131
0

You can try save it in session and then it will stay until the session is time out(20 minutes) if you want it to long time just write it to file in known location and when you close the web write the value to the file and when the web is up again take the vakue from the file.

ilay zeidman
  • 2,654
  • 5
  • 23
  • 45