The situation is as follows: There is a class:
public class Person { private readonly AutoResetEvent _autoEvent = new AutoResetEvent(false); public void Eat() { _taskFactory.StartNew(StartEat); autoEvent.WaitOne() } private void StartEat() { //ΠΠ°ΠΊΠΈΠ΅-ΡΠΎ Π΄Π΅ΠΉΡΡΠ²ΠΈΡ Π½Π° ΠΏΡΠΎΡΡΠΆΠ΅Π½ΠΈΠΈ 10 ΡΠ΅ΠΊΡΠ½Π΄ ΠΌΠΈΠ½ΠΈΠΌΡΠΌ autoEvent.Set(); } } public class Manager { public void Manage() { person.Eat(); } }
There are 2 managers (Manager) and both have a link to the same Person (person). How to make it so that with a simultaneous command to start eating (eat) a person, both managers wait until he eats and returns control. Both managers live in their own streams. Now it turns out that each of the managers will make their own WaitOne, but autoEvent.Set () will be only once for the thread that the first command has given. Is it possible to do without an event?