Module-level variables and multiple threads in IIS

  • Thread starter Thread starter Oenone
  • Start date Start date
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,
 
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)
 
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..?
 
If your module is only used within ASP.NET, you could easily use httpContext.current.items. If you use both ASP.NET and Console for instance, you could do a switching algorithm.

Code:
[ThreadStatic]
Private _Test as String
 
Public property Test() as String
  Get
    dim returnValue as String
    If not httpcontext.current is nothing then
      DirectCast(httpContext.current.items("myVariable"), String)
    else
      returnValue = _Test
    end if
    return returnValue
  End Get
 
  Set(value as String)
    If not httpcontext.current is nothing then
      httpContext.current.items("myVariable") = value
    else
      _Test = value
    end if
  End Set
End Property
 
Back
Top