Why don’t all UI Control methods RunOnUIThread

In both .NET and in Android and I’m sure other frameworks I’ve noticed this issue where you end up with a task that takes too long and so holds up the UI thread.

Say you have a method that updates a label with text from a network location/internet and comes back again after 3-4 seconds and sets the label appropriately. That holds up the UI thread.
So your first port of call then is to move the method call to a different thread.
Only that doesn’t just work… because you can no longer update the text of label since that can only be done in the UI thread….

So then the language provides something along the lines of

      runOnUiThread(() -> {
       label.setText(myNewFoundInformationFrom);
});

That example is from Android using lambda’s and is no doubt a massive improvement on what we might have used 10 years ago…

Maybe further down the line you find yourself doing this from various different places… so you might create a method

void setLabelText(string newText)
 runOnUiThread(() -> {
    label.setText(newText);
    });

and at least in the case of Android… this works no matter which thread I run it from.

So this makes me wonder… Why don’t Microsoft/Google and all make it so that all update methods/functions to UI controls already make sure that they’re running from the UI thread in this way – so that we don’t have to?

0

There are some concerns that may prevent such an always-runInUiThread approach.

The primary problem is that if you’re in a non-UI thread, your operations are deferred by being posted onto a queue, so you have no idea of when your actions will actually take effect. If you have several changes that need to take effect at the same time, you need to group them in a lambda/function anyway and post it as usual, as otherwise another thread may manage to slip in a posted task between your tasks.

This has an analogue in resource synchronization, where it’s incredibly common that you need to perform several operations under the same lock.

Synchronous functions are important, as you sometimes need to know that the result is locked in before continuing.

When you want to change just one label, there is not problem with the solution you have suggested. There are, however, at least two scenarios, in what this makes sense:

  1. Synchronization. Say, you want to update a button text and enable/disable it. With runOnUiThread, there is no need for explicit synchronization or locking.
  2. Bulk update. If you are updating many items, then calling runOnUiThread for every one item would hurt the performance. (Well, if there is horrible amount of items, you should divide them in several batches.)

1

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