Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is not clear from your post whether you are aware that you must modify UI elements on the UI thread - or you will have problems. Here is a brief example which creates a background thread which modifies a TextBox with the current time.</p> <p>The key is MyTextBox.Dispather.BeginInvoke in Page.xaml.cs.</p> <p>Page.xaml:</p> <pre><code>&lt;UserControl x:Class="App.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300" Loaded="UserControl_Loaded"&gt; &lt;Grid x:Name="LayoutRoot"&gt; &lt;TextBox FontSize="36" Text="Just getting started." x:Name="MyTextBox"&gt; &lt;/TextBox&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>Page.xaml.cs:</p> <pre><code>using System; using System.Windows; using System.Windows.Controls; namespace App { public partial class Page : UserControl { public Page() { InitializeComponent(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { // Create our own thread because it runs forever. new System.Threading.Thread(new System.Threading.ThreadStart(RunForever)).Start(); } void RunForever() { System.Random rand = new Random(); while (true) { // We want to get the text on the background thread. The idea // is to do as much work as possible on the background thread // so that we do as little work as possible on the UI thread. // Obviously this matters more for accessing a web service or // database or doing complex computations - we do this to make // the point. var now = System.DateTime.Now; string text = string.Format("{0}.{1}.{2}.{3}", now.Hour, now.Minute, now.Second, now.Millisecond); // We must dispatch this work to the UI thread. If we try to // set MyTextBox.Text from this background thread, an exception // will be thrown. MyTextBox.Dispatcher.BeginInvoke(delegate() { // This code is executed asynchronously on the // Silverlight UI Thread. MyTextBox.Text = text; }); // // This code is running on the background thread. If we executed this // code on the UI thread, the UI would be unresponsive. // // Sleep between 0 and 2500 millisends. System.Threading.Thread.Sleep(rand.Next(2500)); } } } } </code></pre> <p>So, if you want to get things asynchronously, you will have to use Control.Dispatcher.BeginInvoke to notify the UI element that you have some new data.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload