+ Reply to Thread
Results 1 to 2 of 2

Thread: Multiple asynchronous UI updates

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    19

    Multiple asynchronous UI updates

    How does one execute several UI updates from a Silverlight callback? I would like the user clicks the button and the callback seems to execute in the background and then all of the UI changes.

    MainPage.xaml:
    Code:
    <Grid x:Name="LayoutRoot" Background="White" >
    <TextBlock Height="23" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top"/>
    <Button Click="button1_Click" Content="Button" Height="23" Name="button1" Width="75"  />
    </Grid>
    MainPage.xaml.cs:
    Code:
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        textBlock1.Text = "1";
        // also tried Thread.Sleep(5000);
        Dispatcher.BeginInvoke(() => Thread.Sleep(5000));
        textBlock1.Text = "2";
    }

  2. #2
    What you want to do is start a new thread to do your work, then BeginInvoke back to the UI thread:
    Code:
    var thread = new Thread(() => {
      Thread.Sleep(5000);
      Dispatcher.BeginInvoke(() => { 
        // update UI 
        textBox.Text = "2";
      });
    });
    
    textBox.Text = "1";
    thread.Start();

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts