In my multithreading application I am using some variables that can be altered by many instances in the same time. It is weird but it has worked fine without any problem..but of
It's very rare when all you need is just set a single property. More often selectedClient.A = 5
will be a part of a much bigger logical operation, which involves several assignments/evaluations/etc. During that whole operation you'd rather prefer selectedClient
to be in a consistent state and not to introduce deadlocks/race conditions. Therefore, it will be much better to expose SyncRoot
property in your Client
class and lock on that from the calling code:
Client selectedClient = GetClient(...);
lock(selectedClient.SyncRoot)
{
selectedClient.A = 5;
selectedClient.B = selectedClient.A * 54;
}