Page 1 of 1

How to refresh a Binding from another thread ?

Posted: Thu Aug 03, 2017 11:50 pm
by PGilbert
If you fix the following two functions in a )CLEAR WS:

      test;binding;str;types;xaml;xml;⎕USING

⎕USING←'System.IO'
⎕USING,←⊂'System'
⎕USING,←⊂'System.Windows.Markup'
⎕USING,←⊂'System.Xml,system.xml.dll'
⎕USING,←⊂'System.Windows.Controls,WPF/PresentationFramework.dll'

xaml←'<Window '
xaml,←' xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"'
xaml,←' xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" '
xaml,←' xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"'
xaml,←' xmlns:local="clr-namespace:testlibrary" '
xaml,←' Title="Window" Height="300" Width="300"> '
xaml,←' <Grid > '
xaml,←' <StackPanel DataContext="{Binding MyValues}"> '
xaml,←' <TextBox Text="{Binding TextValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></TextBox> '
xaml,←' </StackPanel> '
xaml,←' </Grid> '
xaml,←'</Window>'

str←⎕NEW StringReader(⊂xaml)
xml←⎕NEW XmlTextReader str
win←XamlReader.Load xml

props←⎕NS''
props.MyValues←⎕NS''
props.MyValues.TextValue←'this is working'

binding←(2015⌶)'props'

win.DataContext←binding
win.Show


      refreshBind ra
props.MyValues.TextValue←ra


Then if you execute the function 'test' you get a WPF window as expected with the TextBox text as: 'this is working'. After that if you execute the function 'refreshBind' you can change the value of the TextBox as expected. But if you try to refresh the namespace with 'refreshBind&' from another thread it is not working. Is there a way to make this work ?

      test
refreshBind 'this is working'
refreshBind& 'this is NOT working'


Thanks in advance,

Pierre Gilbert

Re: How to refresh a Binding from another thread ?

Posted: Mon Aug 21, 2017 12:07 pm
by JohnD|Dyalog
Hello Pierre,

This looks like a bug, but there's an easy workaround. Just add another line after the assignment in the function - it doesn't even have to do anything:

Code: Select all

refreshBind ra
props.MyValues.TextValue←ra
⍝hello


The problem is that the binding notifications are kept in a thread-specific queue, and in your code the thread ends before the queue is processed. The extra line gives the interpreter a chance to process the queue before thread termination.

I'll look into a proper fix.

Best Regards,
John Daintree.

Re: How to refresh a Binding from another thread ?

Posted: Mon Aug 21, 2017 12:15 pm
by PGilbert
Thanks John, its working for me too.