Static Keyword

  • Thread starter Thread starter Alvin Bruney
  • Start date Start date
A

Alvin Bruney

I wasn't expecting this behavior from a static variable. A thread wrapped by
a class has a couple static variables in a webform. When the thread runs and
the browser closes, the static variable is still hanging around. I was under
the mistaken impression that static was tied to the session, that it would
go away once the browser closes. It seems to be tied to the application
object instead.

Re-launching the application retrieved the last saved value for the previous
session. I wasn't expecting that. I wonder if I log in with a different
username, will I get that static value from the last session? If so I need
to be worried about synch issues then with multi-users.

Anybody?
 
Alvin Bruney said:
I wasn't expecting this behavior from a static variable. A thread wrapped by
a class has a couple static variables in a webform. When the thread runs and
the browser closes, the static variable is still hanging around. I was under
the mistaken impression that static was tied to the session, that it would
go away once the browser closes. It seems to be tied to the application
object instead.

Re-launching the application retrieved the last saved value for the previous
session. I wasn't expecting that. I wonder if I log in with a different
username, will I get that static value from the last session? If so I need
to be worried about synch issues then with multi-users.

Alvin,

The static variable will be around for the lifetime of the application
domain, since its a web application you'll have to have some type of locking
mechanism unless it is for readonly data, in which case you'll want to make
sure that the initial load is thread safe

hth
andrew
 
Alvin Bruney said:
I wasn't expecting this behavior from a static variable. A thread wrapped by
a class has a couple static variables in a webform. When the thread runs and
the browser closes, the static variable is still hanging around. I was under
the mistaken impression that static was tied to the session, that it would
go away once the browser closes. It seems to be tied to the application
object instead.

It's actually tied to the AppDomain, same as normal. There's nothing
magical about ASP.NET types as opposed to other types.
 
Back
Top