Windows 8 Store Apps Learning (42) Multi-thread thread pool

Source: Internet
Author: User
Tags thread xmlns

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

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.