Steady Silverlight (25)-2.0 thread Thread,timer,backgroundworker,threadpool
Introduced
Silverlight 2.0 uses thread, Timer, BackgroundWorker, ThreadPool to achieve multi-threaded development
Thread-classes for thread creation and control
Timer-class that is used to execute a specified method at a specified time interval
BackgroundWorker-for running an operation on a separate thread
ThreadPool-management class for thread pools
Online Demo
Http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html
Example
1, Thread.xaml
<UserControl x:Class="Silverlight20.Thread.Thread"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Left" Margin="5">
<TextBlock x:Name="txtMsg" />
</StackPanel>
</UserControl>
Thread.xaml.cs
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Net;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Documents;
Using System.Windows.Input;
Using System.Windows.Media;
Using System.Windows.Media.Animation;
Using System.Windows.Shapes;
Namespace Silverlight20.thread
{
public partial class Thread:usercontrol
{
string result = "";
Public Thread ()
{
InitializeComponent ();
Demo ();
}
void Demo ()
{
/**//*
* Thread-classes for thread creation and control
* Name-thread name
* IsBackground-whether it is a background thread (no difference for Silverlight, whether it is a background thread)
* Start (object parameter)-Starts a background thread
* Object parameter-parameters passed to the background thread
* IsAlive-whether the thread is in execution
* Managedthreadid-Unique identifier of the current managed thread
* ThreadState-Specifies the state of the thread [System.Threading.ThreadState enum]
* Abort ()-Terminating threads
*/
DoWork is the method that the background thread executes (omitting the delegate type here)
ThreadStart delegates may not take parameters, Parameterizedthreadstart delegates can take parameters
System.Threading.Thread Thread = new System.Threading.Thread (DoWork);
Thread. Name = "Threaddemo";
Thread. IsBackground = true;
Thread. Start (1000);
result = Thread. IsAlive + "\ r \ n";
result = Thread. Managedthreadid + "\ r \ n";
result = Thread. Name + "\ r \ n";
result = Thread. ThreadState + "\ r \ n";
Thread. Join (); Blocks the calling thread (this example is the main thread) until the specified thread (in this case, thread) finishes execution
Blocking the calling thread (this example is the main thread)
If the specified thread finishes execution, continue (in this case, thread execution is complete)
If the specified thread is running longer than the specified time (in this case, thread execution time will continue if more than 5 seconds)
The return value is the completion of the specified thread in the specified time (in this case, thread execution time is 1 seconds, so returns True)
if (thread. Join (5000))
{
Result + = "Specify the thread to execute within 5 seconds \ r \ n";
}
Txtmsg.text = result;
}
void DoWork (Object sleepmillisecond)
{
System.Threading.Thread.Sleep ((int) sleepmillisecond);
result = = "new thread execution complete \ r \ n";
}
}
}