简体   繁体   中英

Can't make lock object private in static class - why?

In a static class, I have a method which will edit a variable. The class is static because the class is about site detaild and so only one instance is ever required.

Anyway, thread synchronisation is required. I have a lock object, but when I make it private it and say lock (obj){} I get all sorts of errors.

Why is not possible to make the lock object private?

One thought, do you initialise the object statically. Try declaring:

private static object lockObject = new object();

It should work. Are you declaring it as private static ?

private static readonly object lockObject = new object();

public static void Method() {
    lock(lockObject) { 
         // ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM