Windows 8 Store Apps學習(42) 多線程之線程池

來源:互聯網
上載者:User

多線程之線程池: 順延強制, 周期執行, 線上程池中找一個線程去執行指定的方法

介紹

重新想象 Windows 8 Store Apps 之 線程池

通過 ThreadPoolTimer 實現順延強制

通過 ThreadPoolTimer 實現周期執行

通過 ThreadPool 實現“線上程池中找一個線程去執行指定的方 法”

樣本

1、通過 ThreadPoolTimer 實現順延強制(ThreadPoolTimer 在 Windows.System.Threading 命名空間下)

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="120 0 0 0">                            <TextBlock Name="lblMsg" FontSize="14.667" />                            <Button Name="btnCreateDelay" Content="延遲 3 秒後執行一個任務" Click="btnCreateDelay_Click_1" Margin="0 10 0 0" />                <Button Name="btnCancelDelay" Content="取消任務" Click="btnCancelDelay_Click_1" Margin="0 10 0 0" />            </StackPanel>    </Grid></Page>

Thread/ThreadPool/DelayTimer.xaml.cs

/* * 通過 ThreadPoolTimer 實現順延強制(ThreadPoolTimer 在 Windows.System.Threading 命名空間下) *  * ThreadPoolTimer - 計時器 *     ThreadPoolTimer CreateTimer(TimerElapsedHandler handler, TimeSpan delay, TimerDestroyedHandler destroyed); - 建立一個用於順延強制的計時器 *         handler - 指定的延遲時間過後,所需要執行的方法 *         delay - 延遲時間 *         destroyed - 當 ThreadPoolTimer 完成了自身的使命後所執行的方法(比如延遲方法執行完了或計時器被取消了) *     Cancel() - 取消計時器 *     Delay - 延遲時間,唯讀 */    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 ThreadPoolTimer _timer;            public DelayTimer()        {            this.InitializeComponent();        }            // 建立一個延遲計時器        private void btnCreateDelay_Click_1(object sender, RoutedEventArgs e)        {            _timer = ThreadPoolTimer.CreateTimer(                (timer) =>                 {                    var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.High,                        () =>                        {                            lblMsg.Text = "任務執行了";                        });                },                TimeSpan.FromSeconds(3),                 (timer) =>                 {                    var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.High,                        () =>                        {                            lblMsg.Text += Environment.NewLine;                            lblMsg.Text += "ThreadPoolTimer 的使命結束了";                        });                });                lblMsg.Text = "延遲 3 秒後執行一個任務";        }            // 取消計時器        private void btnCancelDelay_Click_1(object sender, RoutedEventArgs e)        {            if (_timer != null)            {                _timer.Cancel();                _timer = null;                    lblMsg.Text += Environment.NewLine;                lblMsg.Text += "任務取消了";            }        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.