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

Registered : 109,027

HOME > บทความจากสมาชิก > (.Net) เขียน Windows App / Console App ตั้งเวลาการทำงาน Schedule Timer (VB.Net,C#)



 
Clound SSD Virtual Server

(.Net) เขียน Windows App / Console App ตั้งเวลาการทำงาน Schedule Timer (VB.Net,C#)

(.Net) เขียน Windows App / Console App ตั้งเวลาการทำงาน Schedule Timer (VB.Net,C#) มีโอกาศได้เขียนโปรแกรมเกี่ยวกับ Schedule หรือตั้งให้โปรแกรมทำงานตามระยะเวลาที่กำหนด เช่น ทำงานทุก ๆ 5,10 นาที หรือทุก ๆ ชม. หรือทำงานวันล่ะหนึ่งครั้ง โดยปกติแล้ว เราสามารถใช้พวกคำสั่ง Timer ที่มาพร้อมกับ .NET Library หรือ อาจจะใช้พวก Windows Task Schedule แต่วิธีนี้มีข้อดีและข้อเสียที่แตกต่างกันไป เช่นถ้าใช้ Timer จะต้องใช้ Time Tick ตั้งเวลาเพื่อตรวจสอบทุก ๆ วินาที ซึ่งวิธีนี้จะให้โห้โปรแกรมทำงาน Load และ Performance ไม่ดีมาก ๆ และถ้าใช้ Windows Task Schedule ก็จะยุ่งยากกับการ Config บน Windows อีก



.Net Application and Schedule Timer


จึงได้ไปหา Library ที่เกี่ยวกับ Schedule และก็ได้เจอ Project ตัวหนึ่งที่ค่อนข้างน่าจะสนใจมาก ความสามารถ รองรับการตั้งเวลาเกือบทุกรูปแบบ ไม่ว่าจะเป็น ระดับ วินาที, นาที, ชั่วโมง, รายวัน และยังสามารถสร้าง Job ได้หลาย ๆ Job อีกด้วย ซึ่งเป็นอะไรที่ตรงกับความต้องการมาก ๆ

Note!! วิธ๊นี้จะต้องเปิดโปรแกรมทิ้งไว้ตลอดเวลา

Download Library
http://www.codeproject.com/Articles/6507/NET-Scheduled-Timer

ให้เลือกดาวน์โหลดตัวที่เป็น Demo เพราะจะมีไฟล์ ScheduleTimer.dll ที่สามารถเรียกใช้งานได้ทันที

Download ScheduleTimer.dll

การเรียกใช้งาน เนื่องจากเป็นไฟล์ .dll ฉะนั้นเราเพียงทำการการ Add Services เข้ามาใน Project ก็สามารถเรียกใช้งานได้เลย

Schedule Timer

ให้ Add ไฟล์ ScheduleTimer.dll เข้ามาใน Project

Schedule Timer

เลือกไฟล์ ScheduleTimer.dll








เขียน Coding ให้ทำการ Import หรือ using คลาสของ Schedule

VB.Net
Imports Schedule

C#
using Schedule;


จากนั้นเรียกใช้งานง่าย ๆ ดังนี้

VB.Net
Imports Schedule

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim Timer = New ScheduleTimer()
        AddHandler Timer.Elapsed, New ScheduledEventHandler(AddressOf timer_Elapsed)
        Timer.AddEvent(New ScheduledTime("Daily", "16:02"))
        Timer.Start()

    End Sub

    Private Sub timer_Elapsed(sender As Object, e As ScheduledEventArgs)
        MessageBox.Show("Schedule Working at 16:02")
    End Sub

End Class


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Schedule;

namespace Test2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ScheduleTimer Timer = new ScheduleTimer();
            Timer.Elapsed += new ScheduledEventHandler(timer_Elapsed);
            Timer.AddEvent(new ScheduledTime("Daily", "16:02"));
            Timer.Start();
        }

        private void timer_Elapsed(object sender, ScheduledEventArgs e)
        {
            MessageBox.Show("Schedule Working at 16:02");
        }
    }
}


จาก Code นี้จะตั้งเวลาให้ทำงาน 16:00 ทุกวัน

Schedule Timer

เปิดหน้าจอ App ให้ทำงาน

Schedule Timer

เมื่อถึงเวลาที่กำหนด ก็จะทำงานตามที่เราต้องการ

ในความสามารถของ Library ตัวนี้รองรับการกำหนดรูปแบบการทำงานที่หลากหลายมาก เช่น

Run every second on the second.
TickTimer.Events.Add(new  Schedule.ScheduledTime("BySecond",  "0"));


Run every minute 15 seconds after the second.
TickTimer.Events.Add(new  Schedule.ScheduledTime("ByMinute",  "15,0"));


Run at 6:00 AM on Mondays.
TickTimer.Events.Add(new  Schedule.ScheduledTime("Weekly",  "1,6:00AM"));


Run once at 6:00 AM on 6/27/08.
TickTimer.Events.Add(new  Schedule.SingleEvent(new DateTime("6/27/2008 6:00")));


Run every 12 minutes starting on midnight 1/1/2003.
TickTimer.Events.Add(new Schedule.SimpleInterval(new 
           DateTime("1/1/2003"), TimeSpan.FromMinutes(12)));


Run every 15 minutes from 6:00 AM to 5:00 PM.
TickTimer.Events.Add(
    new Schedule.BlockWrapper(
        new Schedule.SimpleInterval(new DateTime("1/1/2003"),
                                           TimeSpan.FromMinutes(15)),
        "Daily",
        "6:00 AM",
        "5:00 PM"
    )
);



อ่านเพิ่มเติม
http://www.codeproject.com/Articles/6507/NET-Scheduled-Timer








.


   
Share
Bookmark.   

  By : TC Admin
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2015-12-21
  Download : No files
Sponsored Links
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 05
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 อัตราราคา คลิกที่นี่