Slow and steady Silverlight (25)

Source: Internet
Author: User
Tags execution join thread time interval xmlns silverlight

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";
}
}
}

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.