vendredi 9 janvier 2015

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?





Aucun commentaire:

Enregistrer un commentaire