By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,534 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,534 IT Pros & Developers. It's quick & easy.

Module-level variables and multiple threads in IIS

P: n/a
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,

--

(O)enone
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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)

"Oenone" <oe****@nowhere.com> wrote in message
news:ua**************@TK2MSFTNGP14.phx.gbl...
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,

--

(O)enone

Nov 21 '05 #2

P: n/a
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..?

--

(O)enone
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.