It is necessary to implement access (change / read) from threads to a common variable using the Interlocked class. Code snippets: Common variable:
string bufferMessage = "None"; Writing to variable:
Interlocked.Exchange(ref bufferMessage, "Thread #" + threadName + " WRITE message: " + i); Reading:
messagesRead.Add("Thread #" + threadName + " READ message: " + Interlocked.Read(ref bufferMessage)); Writing is successful, the compiler does not want to skip reading. Displays error:
Не удается преобразовать из "ref string" в "ref long" Is it generally possible to read the value of a string variable using the Interlocked class?
Interlocked.CompareExchange(ref bufferMessage, null, null)- PetSerAl