Module-level variables and multiple threads in IIS

O

Oenone

I'm upgrading a DLL from VB6 to VB.NET. The DLL gets called from an ASP.NET
web application.

In the VB6 code there is a module-level object which stores the context
about what the user is doing during each page request. This object is
initialised at the start of each request and populated with various data
retrieved from the URL, and is then repeatedly queried throughout the rest
of the page generation.

Am I right in thinking that there will be threading issues if I leave this
object in a module in VB.NET? If multiple IIS threads run simultaneously,
will they share the object instance or will they get one each? In VB6 they
got an instance each due to the apartment threading model but I'm suspicious
that this is not the case in VB.NET.

If the instance is shared between threads, this obviously makes it very
likely that the data will be corrupted (after it is initialised for user A,
user B makes a request and overwrites all the data. User A's code continues
to run and picks up all the values that user B's session stored).

Is there an easy way around this?

I looked at the ThreadStatic() attribute and this appears to do nearly what
I want, but it only works for Shared variables. As my object is defined
within a module, there is no need for it to be Shared (and indeed shared
variables aren't allowed in modules).

I'd be grateful for any insight anyone can provide.

My thanks in advance,
 
B

Bruce Barker

you are correct.

in vb6, module variables are created as thread local storage (really nothing
to do with apartment model).

in vb.net they are true global variables shared between all threads. the
thread static attribute, will create cause shared variables to behave as
they did in vb6. but they shoudl not be used with asp.net. this is becuase
asp.net is thread agile, it can switch threads during the processing of a
request. this would cause your variable values to not be stable. indeed if
you use a vb6 com component, you need to set aspcompat mode, so that asp.net
will use the same thread to access the com component.

-- bruce (sqlwork.com)
 
O

Oenone

Hi Bruce,
in vb.net they are true global variables shared between all threads.
the thread static attribute, will create cause shared variables to
behave as they did in vb6. but they shoudl not be used with asp.net.
this is becuase asp.net is thread agile, it can switch threads during
the processing of a request.

Hmm, I wasn't aware of that -- just when I thought things were complicated
enough, a little bit more complexity comes along! :-/

How exactly should I go about using variables defined in modules then? From
what you're saying and what I'm reading about agile threads, it doesn't look
like this is going to be possible..?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top