ObservableCollection<MyClass> collection = new ObservableCollection<MyClass>();
public MainWindow()
{
InitializeComponent();
collection.Add(new MyClass() { Name = "Vikram" });
collection.Add(new MyClass() { Name = "Test" });
CallingTaskDelay();
MyClass class1 = collection[0];
lock (class1)
{
txtName.Text = class1.Name;
}
}
private Task TaskDelayMethod()
{
return Task.Run(() => ChangeItem());
}
private async void CallingTaskDelay()
{
await TaskDelayMethod();
}
private void ChangeItem()
{
MyClass myclass = collection[0];
lock (myclass)
{
myclass.Name = "anup";
Thread.Sleep(5000);
}
}
While I am changing the content of the instance of the myclass in ChangeItem() method. I want to prevent access of the same instance in the MainWindow() method which I am doing as below -
lock (class1)
{
txtName.Text = class1.Name;
}
How it can be accomplished. Mine is not working.