threadin issues.

  • Thread starter Thread starter Ashish
  • Start date Start date
A

Ashish

Hi All,
I am designing a class library that can be used by web and windows
client both, the problem is that i want to initiate certain behavior in
class that is thread specific, yet i want to declare a static methods
for this behavior....

doing some research online i found out that one can use Thread Local
Storage or mark a variable with ThreadStatic attribute, I also found
that both methods are not recommended for use with ASP.net applications
because same thread can be used serve multiple request, and threads come
from a pool ..

anyway other good way to accomplish it , keeping its applicability
transparent ?


TIA
-ashish
 
it all depends on what your class is designed to do. If you are going to
share global variables you will need to protect them. otherwise you don't
need to worry about threading.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
Ashish,

When developing a class library you normally make sure all static
members are thread-safe. Instance members are not typically
thread-safe because each thread can (and usually does) create its own
instance of that class that is not shared by other threads. If more
than one thread needs to share the same instance then it's the
responsibility of the callers to synchronization access to the instance
members. This is the general pattern Microsoft used in the base class
library.

I don't recommend using any thread specific techniques including Thread
Local Storage or the ThreadStatic attribute. What behavior is it that
you want to be thread specific?

Brian
 
I have implemented a DataBase Manager, that saves all the objects
registered with it.

So the client can just register all the objects it wants to save,Delete
with this Manager, and then flushes the manager (much like a O/R Session)

I would like to create a transaction object and add it to the call
context or current thread, then all the database operations can check
whether there is a transaction object in the call context, this would
save me from passing the transaction object reference around, and make
the code much cleaner.

I can use [ThreadStatic] attribute, but threads gets reused, on another
note Is a thread dedicated to the whole life time of a request ?

anyways any pointers would be appreciated.

TIA
-ashish
 
Ashish said:
I have implemented a DataBase Manager, that saves all the objects
registered with it.

So the client can just register all the objects it wants to save,Delete
with this Manager, and then flushes the manager (much like a O/R Session)

I would like to create a transaction object and add it to the call
context or current thread, then all the database operations can check
whether there is a transaction object in the call context, this would
save me from passing the transaction object reference around, and make
the code much cleaner.

Honestly, passing around a transaction reference would be a better
approach. Since you say you have a database manager object then
couldn't you encapsulate the transaction handling there? That way you
wouldn't have to keep passing around the reference.
I can use [ThreadStatic] attribute, but threads gets reused, on another
note Is a thread dedicated to the whole life time of a request ?

I'd say it's likely that a request is serviced by a single thread, but
I can't tell you that for sure.
 

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

Back
Top