Prevent deadlock with two properties dependent on each other

How do I change two observable properties, based on each other, but prevent a deadlock?

For the sake of this example I’m keeping it very simple:
I’m counting two things, (1) items and (2) containers for these items. Every container can contain 3 items. The user can change both the amount of items and the amount of containers. Changing one will automatically change the other.

As you can imagine, this results in a complete deadlock.

Displaying values:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><Entry Text="{Binding Amount}"/>
<Entry Text="{Binding Sections}"/>
</code>
<code><Entry Text="{Binding Amount}"/> <Entry Text="{Binding Sections}"/> </code>
<Entry Text="{Binding Amount}"/>
<Entry Text="{Binding Sections}"/>

Setting values:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private int amount;
public int Amount
{
get => amount;
set
{
SetProperty(ref amount, value);
Sections = Convert.ToInt32(Math.Round(Amount / 3));
}
}
private int sections;
public int Sections
{
get => sections;
set
{
SetProperty(ref sections, value);
Amount = Sections * 3;
}
}
</code>
<code>private int amount; public int Amount { get => amount; set { SetProperty(ref amount, value); Sections = Convert.ToInt32(Math.Round(Amount / 3)); } } private int sections; public int Sections { get => sections; set { SetProperty(ref sections, value); Amount = Sections * 3; } } </code>
private int amount;
public int Amount
{
    get => amount;
    set
    {
        SetProperty(ref amount, value);
        Sections = Convert.ToInt32(Math.Round(Amount / 3));
    }
}

private int sections;
public int Sections
{
    get => sections;
    set
    {
        SetProperty(ref sections, value);
        Amount = Sections * 3;
    }
}

How do I prevent a deadlock, e.g. changing the properties only once when it is user-invoked?

2

You can implement a flag so that only the property you are setting triggers the setting of the other. You set sections, it updates sections and amount, but amount does not trigger the sections update.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class TestClass
{
private bool _updating = false;
private int _amount;
public int Amount
{
get => _amount;
set
{
_amount = value;
if (!_updating)
{
_updating = true;
Sections = Convert.ToInt32(Math.Round(Convert.ToDouble(Amount / 3)));
_updating = false;
}
}
}
private int _sections;
public int Sections
{
get => _sections;
set
{
_sections = value;
if (!_updating)
{
_updating = true;
Amount = _sections * 3;
_updating = false;
}
}
}
}
</code>
<code>public class TestClass { private bool _updating = false; private int _amount; public int Amount { get => _amount; set { _amount = value; if (!_updating) { _updating = true; Sections = Convert.ToInt32(Math.Round(Convert.ToDouble(Amount / 3))); _updating = false; } } } private int _sections; public int Sections { get => _sections; set { _sections = value; if (!_updating) { _updating = true; Amount = _sections * 3; _updating = false; } } } } </code>
public class TestClass
{
    private bool _updating = false;

    private int _amount;
    public int Amount
    {
        get => _amount;
        set
        {
            _amount = value;
            if (!_updating)
            {
                _updating = true;                 
                Sections = Convert.ToInt32(Math.Round(Convert.ToDouble(Amount / 3)));
                _updating = false;
            }
        }
    }

    private int _sections;
    public int Sections
    {
        get => _sections;
        set
        {
            _sections = value;
            if (!_updating)
            {
                _updating = true;
                Amount = _sections * 3;
                _updating = false;
            }
        }
    }
}

You can then call the setters without having to worry about the infinite loop:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>static void Main()
{
var tc = new TestClass();
Console.WriteLine("Set sections = 5");
tc.Sections = 5;
Console.WriteLine($"Sections: {tc.Sections}; Amount: {tc.Amount}");
Console.WriteLine("Set amount = 10");
tc.Amount = 10;
Console.WriteLine($"Sections: {tc.Sections}; Amount: {tc.Amount}");
}
</code>
<code>static void Main() { var tc = new TestClass(); Console.WriteLine("Set sections = 5"); tc.Sections = 5; Console.WriteLine($"Sections: {tc.Sections}; Amount: {tc.Amount}"); Console.WriteLine("Set amount = 10"); tc.Amount = 10; Console.WriteLine($"Sections: {tc.Sections}; Amount: {tc.Amount}"); } </code>
static void Main()
{
    var tc = new TestClass();

    Console.WriteLine("Set sections = 5");

    tc.Sections = 5;

    Console.WriteLine($"Sections: {tc.Sections}; Amount: {tc.Amount}");

    Console.WriteLine("Set amount = 10");

    tc.Amount = 10;

    Console.WriteLine($"Sections: {tc.Sections}; Amount: {tc.Amount}");
}

Yields this result:

Set sections = 5

Sections: 5;
Amount: 15

Set amount = 10

Sections: 3;
Amount: 10

create two methods to update the properties

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>void SetAmount(int amount)
{
this.Amount = amount;
Sections = Convert.ToInt32(Math.Round(Amount / 3));
}
void SetSections(int sections)
{
this.Sections = sections;
Amount = Sections * 3;
}
</code>
<code>void SetAmount(int amount) { this.Amount = amount; Sections = Convert.ToInt32(Math.Round(Amount / 3)); } void SetSections(int sections) { this.Sections = sections; Amount = Sections * 3; } </code>
void SetAmount(int amount)
{
  this.Amount = amount;
  Sections = Convert.ToInt32(Math.Round(Amount / 3));
}

void SetSections(int sections)
{
  this.Sections = sections;
  Amount = Sections * 3;
}

this way the properties setters are not recursively calling each other

Rather than having two separate values and trying to maintain data integrity between them, just have only one stored value and let one of the properties be an entirely computed value. This not only means that there’s no recursion to address, but there’s no possible state for the object that’s “invalid”.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private int amount;
public int Amount
{
get => amount;
set
{
SetProperty(ref amount, value);
}
}
public int Sections
{
get => (int)Math.Round(Amount / 3);
set => Amount = Sections * 3;
}
</code>
<code>private int amount; public int Amount { get => amount; set { SetProperty(ref amount, value); } } public int Sections { get => (int)Math.Round(Amount / 3); set => Amount = Sections * 3; } </code>
private int amount;
public int Amount
{
    get => amount;
    set
    {
        SetProperty(ref amount, value);
    }
}

public int Sections
{
    get => (int)Math.Round(Amount / 3);
    set => Amount = Sections * 3;
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật