Methods for updating the UI by non-UI threads in Silverlight

Source: Internet
Author: User
Methods for updating the UI by non-UI threads in Silverlight: Delegate, AsyncOperation, and BackgroundWorker

First, list the basic code: <UserControl
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: d = "http://schemas.microsoft.com/expression/blend/2008"
Xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
Mc: Ignorable = "d"
X: Class = "using Ach. TestUI. UCThreadUpdate"
D: DesignWidth = "250" d: DesignHeight = "120">
<StackPanel>
<TextBlock x: Name = "txtCalc"/>
</StackPanel>
</UserControl> using System;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Documents;
Using System. Windows. Ink;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Animation;
Using System. Windows. Shapes;
Using System. Threading;
Namespace using Ach. TestUI
{
Public partial class UCThreadUpdate: UserControl
{
Public UCMsgSend ()
{
InitializeComponent ();
ThreadUpdate ();
}
Void ThreadUpdate ()
{
Thread thread = new Thread (NewThreadStart (DoWork ));
Thread. Start ();
}
Void DoWork ()
{
Int I = 0;
While (I <100 ){
DoShow (I );
}
}
}
}

Three DoShow statements
1. delegateVoid DoShow (I ){
This. Dispatcher. BeginInvoke (
Delegate {
TxtCalc. Text = string. format ("result" {0} ", I );
});
}

2. AsyncOperationVoid DoShow (I ){
// This can be written as a member variable. Here I just want to differentiate
System. ComponentModel. AsyncOperation asyncOper = System. ComponentModel. AsyncOperationManager. CreateOperation (null );
AsyncOper. Post (result =>
{
TxtCalc. Text = string. format ("result" {0} ", I );
}, Null );
}

3. BackgroundWorkerRefer to the document on MSDN

The same is true for Winform,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.