Module-level variables and multiple threads in IIS

Discussion in 'ASP .Net' started by Oenone, Jun 2, 2005.

  1. Oenone

    Oenone Guest

    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
    Oenone, Jun 2, 2005
    #1
    1. Advertising

  2. Oenone

    Bruce Barker Guest

    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" <> wrote in message
    news:...
    > 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
    >
    Bruce Barker, Jun 2, 2005
    #2
    1. Advertising

  3. Oenone

    Oenone Guest

    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
    Oenone, Jun 2, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    440
  2. J.K. Bach
    Replies:
    0
    Views:
    353
    J.K. Bach
    Jun 15, 2005
  3. Mudcat
    Replies:
    3
    Views:
    941
    Steven D'Aprano
    Jan 14, 2006
  4. Replies:
    1
    Views:
    319
    Diez B. Roggisch
    Oct 29, 2006
  5. pabbu
    Replies:
    8
    Views:
    719
    Marc Boyer
    Nov 7, 2005
Loading...

Share This Page