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,