Reputation: 505
I have Asp.net mvc application and I need global counter for some purpose for all users(and guests). I mean any user can change it(sometimes from different threads), but this counter is common for all of them. So,
1. is it good idea to create just static variable somewhere in my code for this ?
2. Are there possible problems?
Any other suggestions? Thanks a lot!
Upvotes: 3
Views: 1753
Reputation: 16389
Its tricky to say anything here. Static in itself do not have problem. It depends on how you are using it. As you said you are putting some global counter in variable. So multiple threads will modify it; right? Make sure this call is thread-safe. Otherwise the counters will be wrong.
If this was some rarely written and mostly read-only value, it was not an issue at all.
If this is a value known to you at design time, better you use const
.
You should also consider IIS recycles application pool. In that case, new Application instance will be created and two copies of your static variable will exist.
If this all does not create problem for you then static is ok.
Upvotes: 3