I have DPs a and b to which c is bound via i converter (note that a and b might be bound to another DP via a converter). I modify a and b or some of the DPs that they are bound to and then use c in a calculation. I do this in a for loop, and it is taking a really long time, the conditions is i=0; i<100000; i++). So I am wondering how efficient is data binding? And should it be used in scenarios like this?
Here is some sample code:
for ( int i = 0; i < 100000; i++){
//... code to pick m based on some random numbers
hazards[m].Reactant1.Count -= 1;
hazards[m].Reactant2.Count -= 1;
hazards[m].Product.Count += 2;
display.Text = hazards[m].Value.ToString();
}
hazards.Value is bound to the count of the reactants via a converter, the count of the reactants is bound to a textbox text property. m is picked based on the hazard value and some random numbers.
Long running task should never be run in the UI thread. If you run your long running calculation here your UI will be slow – independently of the speed of data binding. Since you block the updates of the UI.
If you change the value in a background thread then you have to use a dispatcher who makes a thread switch for you. This again comes with an overhead.
In both ways you have many factors who will slow down your application.
And it doesn’t really say anything reliable on the efficiency of data binding.
I suggest you write your code in a background thread and check if it is fast enough. If not use a profiler to check where your bottleneck is.