C # can lock method parameter?

The class has a class variable ArrayList binaryScanData. In all methods that have access to it, we put on it lock(binaryScanData)because it is shared. Now we want to transfer one of these methods to another util class to make it a method static. We will pass that binaryScanDatato a method like this:

public static void convertAndSaveRawData(ref MemoryStream output, ref ArrayList binaryScanData)

Our questions follow:

  • How can we sync this binaryScanData? can we do the same as originally?
  • refis it necessary? It will be read only in this method convertAndSaveRawData.
+3
source share
6 answers

, , (.. ). , . SyncRoot, , ArrayList. , List<T>.

+4

ref, binaryScanData = something.

, - .
# lock keyowrd ; , .

+4

. , ArrayList , , . ArrayList , ?

, , , , ArrayList. , . . , ArrayList.

, . , ArrayList . , , .

+2
  • ScanData? , ?

, .

, , . . , - . .

  1. ref ? convertAndSaveRawData.

, . .

+1

ArrayList, get/sets:

ArrayList unsyncList = new ArrayList();
ArrayList syncWrapperList = ArrayList.Synchronized(unsyncList);

, , .

0

ref , , .

, ArrayList , . List<T> , 1.1

: - , .NET . : # List < > ? , .

One way, if you created the list yourself, is to make it a read-only list and provide methods to add to it by subclassing List<T>or IList<T>.

This ThreadSafe List tries to create a common thread-safe list, but if you read the comments at the bottom of the page, there are still flaws.

0
source

Source: https://habr.com/ru/post/1743200/


All Articles