Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / November 2005

Tip: Looking for answers? Try searching our database.

Threading Issues

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ashish - 09 Nov 2005 00:05 GMT
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
David Browne - 09 Nov 2005 19:44 GMT
> Hi All,
> I am designing a class library that can be used by web and windows client
[quoted text clipped - 10 lines]
> anyway other good way to accomplish it , keeping its applicability
> transparent ?

You'll have to be a little more forthcoming with what you're hoping to
acomplish.  ThreadStatic can work well in ASP.NET for some things.  You just
have to understand that your threads are in the ASP.NET worker thread pool,
and have no session affinity.

David
Ashish - 09 Nov 2005 23:38 GMT
David,

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 (or add it to the TLS), but threads
gets reused,
on another note Is a thread dedicated to the whole life time of a
request, or does it interleaves between multiple requests ?

anyways any comments would be appreciated.

TIA
-ashish

Brian Gideon wrote:
> Ashish,
>
[quoted text clipped - 30 lines]
>>TIA
>>-ashish

>>Hi All,
>>I am designing a class library that can be used by web and windows client
[quoted text clipped - 17 lines]
>
> David
David Browne - 10 Nov 2005 03:20 GMT
> David,
>
[quoted text clipped - 14 lines]
> on another note Is a thread dedicated to the whole life time of a request,
> or does it interleaves between multiple requests ?

ThreadStatic is perfect for this.  A thread will be assigned to your
request, and the same thread will handle the whole request.  In an outer
scope you will need to establish the call context and save it into a
ThreadStatic variable.  Then call into whatever code you want to service
with the call context, and on return commit the transaction, and on any
exception, roll back.  Something like

class DBContext
{
 [ThreadStatic]
 public static DBContext CurrentContext;
 . . .

 public readonly SqlConnection Connection;
 . . .
}

. . .

DBContext.CurrentContext = new DBContext(...);
try
{
 //do whatever
 DBContext.CurrentContext.Commit();
}
catch (Exception ex)
{
 DBContext.CurrentContext.Rollback();
 throw ex;
}
finally
{
 DBContext.CurrentContext.Connection.Close();
 DBContext.CurrentContext = null;
}

Then anywhere in the call stack of the invoked components you can access the
CurrentContext's Connection with

 DBContext.CurrentContext.Connection

David

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.