Re: Microsoft Data Access Block and static methods
From: Scott Allen (bitmask_at_[nospam)
Date: 12/10/04
- Next message: localhost: "Re: Fastest DataRow Copy?"
- Previous message: Elton Wang: "Crystal Report and password protected Access database"
- In reply to: Clint: "Re: Microsoft Data Access Block and static methods"
- Next in thread: Clint: "Re: Microsoft Data Access Block and static methods"
- Reply: Clint: "Re: Microsoft Data Access Block and static methods"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 10 Dec 2004 09:43:06 -0500
Hi Clint:
>
>So I don't have to worry about adding my own lock(...) commands when
>referencing MS-written static methods, but if I'm writing my own static
>methods that reference static fields in a class, I should use
>lock(...)?
>ie,
>
It's always good to check the docs, but generally the MS static
methods are thread safe.
>class Foo
>{
>private static string testValue;
>
>public static void AdjustTestValue()
>{
>lock (this) // or lock(typeof(Foo))?
>{
>testValue = "something";
>}
>}
>}
Yes, a lock would be needed. Remember you can't use "this" in a static
method. Jon has a good article on picking what to lock on:
http://www.yoda.arachsys.com/csharp/multithreading.html#lock.choice
>
>----------
>That being said, instance members are not safe because a single
>instance of an object is typically not going to be used by multiple
>threads. In ASP.NET for instance, you typically create an instance of a
>class for each user request - a TextBox control will never see multiple
>threads. Since locks add some performance overhead (and a lot of
>development and testing overhead), the decision makes a great deal of
>sense, even if it looks counterintuitive at first.
>----------
>
>I'm having trouble understanding this ... do you mean that in an
>ASP.NET (and, for my purpose, Web Services), I should be instantiating
>classes instead of using statics, or that it's ok to use statics in
>that each request is processed on it's own thread, so long as I don't
>actually STORE a value in a static field(ex, inside the static method,
>I perform a SQL query only, not storing the result of that query to a
>static field)?
>
It is OK to use static methods in a multithreaded app, the place to be
very careful is when using static fields.
-- Scott http://www.OdeToCode.com/blogs/scott/
- Next message: localhost: "Re: Fastest DataRow Copy?"
- Previous message: Elton Wang: "Crystal Report and password protected Access database"
- In reply to: Clint: "Re: Microsoft Data Access Block and static methods"
- Next in thread: Clint: "Re: Microsoft Data Access Block and static methods"
- Reply: Clint: "Re: Microsoft Data Access Block and static methods"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|