Multi-thread thread pool: Deferred execution, cycle execution, find a thread in the threads pool to execute the specified method
Introduced
Re-imagine the Windows 8 Store Apps thread pool
Implementing deferred execution via Threadpooltimer
Implement cycle execution through Threadpooltimer
Using the ThreadPool implementation "find a thread in the threads pool to execute the specified method"
Example
1. Implement deferred execution via Threadpooltimer (Threadpooltimer under Windows.System.Threading namespace)
Thread/threadpool/delaytimer.xaml
<page
x:class= "XamlDemo.Thread.ThreadPool.DelayTimer"
xmlns= "http://schemas.microsoft.com/winfx/ 2006/xaml/presentation "
xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:local=" using: XamlDemo.Thread.ThreadPool "
xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "
xmlns:mc=" http://schemas.openxmlformats.org/markup-compatibility/2006 "
mc:ignorable=" D ">
<grid background=" Transparent ">
<stackpanel margin=" 0 0 0 ">
<textblock name=" lblmsg "fontsize=" 14.667 "/>
<button name= "Btncreatedelay" content= "delay 3 seconds to perform a task" click= "Btncreatedelay_click_1" margin= "0 0 0"/>< C12/><button name= "Btncanceldelay" content= "Cancel Task" click= "Btncanceldelay_click_1" margin= "0 0 0"/> </
stackpanel>
</Grid>
</Page>
Thread/threadpool/delaytimer.xaml.cs
* * Implement deferred execution via Threadpooltimer (Threadpooltimer under Windows.System.Threading namespace) * * Threadpooltimer-timer * THR Eadpooltimer Createtimer (Timerelapsedhandler handler, TimeSpan delay, timerdestroyedhandler destroyed); -Create a timer for deferred execution * Handler-after the specified delay time, the method required to execute * delay-delay time * destroyed-when Threadpooltimer
The method executed after the completion of its own mission (for example, the delay method is executed or the timer is canceled) * Cancel ()-Cancels the timer * Delay-delay time, read only/using System;
Using Windows.UI.Xaml;
Using Windows.UI.Xaml.Controls;
Using Windows.System.Threading;
Using Windows.UI.Core; Namespace XamlDemo.Thread.ThreadPool {public sealed partial class Delaytimer:page {private Threa
Dpooltimer _timer; Public Delaytimer () {this.
InitializeComponent ();
//Create a delay timer private void Btncreatedelay_click_1 (object sender, RoutedEventArgs e) {
_timer = Threadpooltimer.createtimer (timer) => {var ignored = Dispatcher.runasync (Coredispatcherpriority.high,
() => {lblmsg.text = "task executed";
}); }, Timespan.fromseconds (3), (timer) => {var Igno
Red = Dispatcher.runasync (Coredispatcherpriority.high, () => {
Lblmsg.text + = Environment.NewLine;
Lblmsg.text = "Threadpooltimer's mission is over";
});
});
Lblmsg.text = "Delay 3 seconds to perform a task";
}//Cancel timer private void Btncanceldelay_click_1 (object sender, RoutedEventArgs e) { if (_timer!= null) {_timer.
Cancel ();
_timer = null; Lblmsg.text + + ENVIRONMENT.NEWLIne
Lblmsg.text + = "Mission Canceled"; }
}
}
}