Re: Thread Locking In Static Methods - How?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: John M Deal (johndeal_at_necessitysoftware.com)
Date: 11/15/04


Date: Mon, 15 Nov 2004 07:47:34 -0800

No. What this does is cause a lock of any code location that uses the
same lock definition. If every method in the class had its code
encompassed by the same statement it would cause the whole class to lock
anytime one of the methods was entered, but only if that was the case.

If you want to make sure that only the one method is locked you could
create a private static member variable for the specific method, then
lock against that. For example:

private static string _logTextLock = "This is used to lock.";
public static void LogText(string text)
{
        lock (_logTextLock)
        {
                ...
        }
}

Hope that helps.

Have A Better One!

John M Deal, MCP
Necessity Software

McFly Racing wrote:
> If I do it this way it seems like I will be loking the whole class instead
> of just this method, is that correct? Thank you.
>
> "ShaneB" <stormfire1@yahoo.com> wrote in message
> news:OR$PReyyEHA.1564@TK2MSFTNGP09.phx.gbl...
>
>>public static void LogText(string text)
>>{
>> lock (typeof(ThisClassType))
>> {
>> ...
>> }
>>}
>>
>>ShaneB
>>
>>"McFly Racing" <NoSpam@Microsoft.com> wrote in message
>>news:ed2F8XyyEHA.2568@TK2MSFTNGP10.phx.gbl...
>>
>>>Thread Locking In Static Methods
>>>
>>>
>>>
>>>I have the need for a Log Manger class that has static methods.
>
> Normally
>
>>>I
>>>would use the lock statement or a Monitor statement both of which take a
>>>reference to (this). In the case of these static methods I am not able
>
> to
>
>>>do that.
>>>
>>>
>>>
>>>How should I perform thread locking within static methods like this?
>>>Thank
>>>you.
>>>
>>>
>>
>>
>
>



Relevant Pages

  • Re: Threading newbie: Lock statement
    ... Can I use the lock statement to ... // At the same time I have Thread2 executing on MySecondThreadProcedure() ... public static void MySecondThreadProcedure() ... An integer is not a reference type, so you will be locking on a boxed copy ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threading newbie: Lock statement
    ... having a value type as the lock expression will cause a compilation error. ... public static void MySecondThreadProcedure() ... An integer is not a reference type, so you will be locking on a boxed ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threading newbie: Lock statement
    ... If some other thread tries to create another Monitor using the same object, it will not be allowed to proceed until the first Monitor is released. ... Can I use the lock statement to synchronise access, or can I only use lock when the threads access the ressource from the *same* part of he code. ... public static void MySecondThreadProcedure() ... The locking statements have no effect what so ever, as you will be locking on a newly created object every time. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threading newbie: Lock statement
    ... having a value type as the lock expression will cause a compilation error. ... Can I use the lock statement to synchronise ... public static void MySecondThreadProcedure() ... An integer is not a reference type, so you will be locking on a boxed ...
    (microsoft.public.dotnet.languages.csharp)
  • Threading newbie: Lock statement
    ... I have two threads accessing the same ressource, ... Can I use the lock statement to synchronise ... public static void MySecondThreadProcedure() ...
    (microsoft.public.dotnet.languages.csharp)