Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

Windows Phone and Timer (Trigger, Tick, DispatcherTimer)

Windows Phone and Timer (Trigger, Tick, DispatcherTimer) ในการเขียนโปรแกรมเกือบทุกภาษาจะต้องมี Timer เข้ามาเกี่ยวข้อง ประโยชน์ของ Timer คือใช้สำหรับการกำหนดหรือสร้าง Trigger หรือเรียก Event ต่าง ๆ ที่ต้องการให้โปรแกรมทำงานตามระยะหรือทุก ๆ ครั้งที่ต้องการ เช่น ในขณะที่เปิดหน้าจอ Application อยู่นั้น เราต้องการตรวจสอบว่าระหว่างนั้นได้เกิดเหตุการณ์อะไรขึ้น ระหว่าง Application กับ Server เช่น มีข้อมูลใหม่เข้ามาหรือไม่ หรือมีข้อความที่ต้องการแจ้งให้ผู้ใช้ทราบทางหน้าจอ ซึ่งเหตุการณ์เหล่า ๆ นี้เราไม่สามารถเขียนโปรแกรมสร้าง Event ต่าง ๆ ได้ เพราะ Event ส่วนมากจะต้องเกิดเมื่อผู้ใช้ได้กระทำการต่าง ๆ บนหน้าจอ Application และจะหายไปหลังจากทำงานเสร็จสิ้น

Windows Phone and Timer

Windows Phone and Timer


เพราะฉะนั้นการที่จะสั่งให้โปรแกรมทำงานทุก ๆ ครั้ง หรือตามระยะเวลาที่กำหนด เช่น สั่งให้ไปตรวจสอบข้อมูลจาก Server ทุก ๆ 5-10 วินาที ซึ่ง Event เหล่านี้เราสามารถใช้ Timer เข้ามาสร้าง Trigger ได้ และการใช่ Timer ยังสามารถประยุกต์ใช้กับพวกข้อมูลแบบ Realtime ในการติดต่อกับ Server แต่ข้อควรจะวังการใช้ Timer จะต้องศึกษาเรื่อง Performance ของ Process ด้วย เพราะถ้าใช้ Timer มากจนเกินไปก็จะก่อให้เกิด Process มากมายที่เกิดขึ้น ผลที่ตามมาคือโปรแกรมอาจจะแฮ้งหรือช้า หรือ Memory leak เป็นต้น

ในการใช้ Timer บน Windows Phone จะเรียกใช้ Class ที่มีชื่อว่า DispatcherTimer ซึ่งเป็น Class ที่อยู่ภายใต้ NameSpace ของ System.Windows.Threading เพราะฉะนั้นการทำงานของ Timer จะเป็นในรูปแบบของ Thread และ Background Process โดยปริยาย

VB.NET
	Dim dt As DispatcherTimer = New DispatcherTimer

    ' Constructor
    Public Sub New()
        InitializeComponent()

        dt.Interval = New TimeSpan(0, 0, 0, 0, 1000)
        AddHandler dt.Tick, AddressOf Me.dt_Tick
        dt.Start()
    End Sub

    Private Sub dt_Tick(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

C#
DispatcherTimer dt = new DispatcherTimer();
// Constructor
public New()
{
	InitializeComponent();

	dt.Interval = new TimeSpan(0, 0, 0, 0, 1000);
	dt.Tick += this.dt_Tick;
	dt.Start();
}

private void dt_Tick(object sender, EventArgs e)
{
}

จาก Code มีการประกาศตัวแปรชื่อว่า dt ซึ่งเป็นชนิด DispatcherTimer และกำหนด Interval จาก TimeSpan ให้ทำงานทุก ๆ 1 วินาที (1000=1 วินาที) จากนั้นให้กำหนด Trigger Tick ไปยัง Event Handler ที่มีชื่อว่า dt_Tick ซึ่งจาก Code ในส่วนของ Sub dt_Tick() จะทำงานทุก ๆ 1 วินาที


Example ตัวอย่างการใช้ Timer ในการกำหนด Trigger ให้โปรแกรมแสดงเวลาบนหน้าจอ Application ของ Windows Phone

ออกแบบหน้าจอ Layout ดังภาพ

Windows Phone and Timer

     <!--LayoutRoot contains the root grid where all other page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Height="46" HorizontalAlignment="Left" Margin="9,253,0,0" Name="txtResult" Text="Result" VerticalAlignment="Top" Width="441" TextAlignment="Center" FontSize="26" />
        </Grid>

    </Grid>









VB.NET
Imports System.Windows.Threading

Partial Public Class MainPage
    Inherits PhoneApplicationPage

    ' Constructor
    Public Sub New()
        InitializeComponent()

        dt.Interval = New TimeSpan(0, 0, 0, 0, 1000)
        AddHandler dt.Tick, AddressOf Me.dt_Tick
        dt.Start()
    End Sub

    Dim dt As DispatcherTimer = New DispatcherTimer

    Private Sub dt_Tick(ByVal sender As Object, ByVal e As EventArgs)
        Me.txtResult.Text = Date.Now()
    End Sub

End Class


C#
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;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;

namespace PhoneAppCS
{
    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            dt.Interval = new TimeSpan(0, 0, 0, 0, 1000);
            dt.Tick += new System.EventHandler(this.dt_Tick);
            dt.Start();

        }

        private DispatcherTimer dt = new DispatcherTimer();

        private void dt_Tick(object sender, EventArgs e)
        {
            this.txtResult.Text = DateTime.Now.ToString();
        }
    }
}

Code สำหรับ VB.NET และ C# ที่จะควบคุมการทำงานของโปรแกรม

Screenshot ทดสอบโปรแกรมบน Emulator

Windows Phone and Timer

แสดงเวลาของเครื่อง SmartPhone

Windows Phone and Timer

แสดงเวลาของเครื่อง SmartPhone แบบ Realtime







.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-09-04 21:51:10 / 2017-03-25 21:58:52
  Download : Download  Windows Phone and Timer (Trigger, Tick, DispatcherTimer)
 Sponsored Links / Related

 
Windows Phone and Thread (Silverlight)
Rating :

 
Windows Phone and ProgressBar Using Thread/Multiple Thread (Silverlight)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่