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 > Windows Service การเขียนโปรแกรมด้วย .Net Application และการสร้าง Windows Services > ตอนที่ 4 : การทำ Job Schedule Time ตั้งเวลาทำงานบน Windows Service (VB.Net,C#)



Clound SSD Virtual Server

ตอนที่ 4 : การทำ Job Schedule Time ตั้งเวลาทำงานบน Windows Service (VB.Net,C#)

การทำ Job Schedule Time ตั้งเวลาทำงานบน Windows Service (VB.Net,C#) การจะทำให้ Windows Service ทำงานแบบตลอดเวลาตามระยะเวลาที่กำหนด ในบทความก่อนหน้านี้เราจะใช้ Timer ในการกำหนดให้ Windows Service ทำงานเป็น Loop แล้วค่อยมาเขียนเงื่อนไขเพื่อให้โปรแกรมทำงานต่างๆ ตามที่ต้องการ แต่ข้อเสียของการใช้ Timer คือ โปรแกรมอาจจะทำงานโดยที่ไม่จำเป็น เช่น ถ้าเราต้องการให้โปรแกรมทำงานทุกๆ วัน ตอนเวลา 10:00 น. ในกรณีที่ใช้ Timer อาจจะต้องตั้งเวลาทุกๆ 5 วินาที แล้วค่อยตรจสอบว่าเวลาปัจจุบันถึง 10:00 แล้วหรือยัง วิธีนี้จะเห็นว่าเราต้องการให้โปรแกรมทำงานแค่ครั้งเดียวตอนเวลา 10:00 น. แต่ต้องใช้ Timer เช็คทุกๆ 5 วินาทีเพื่อคอยตรวจสอบเวลา ซึ่งจะเห็นว่าเกิด Process การทำงานที่ไม่จำเป็น นับหลายร้อยครั้ง และอาจจะทำให้ Performance ของ โปรแกรมลดลงไปอีกด้วย



การทำ Job Schedule เป็นปัญหาของการเขียนโปรแกรม เพราะปัจจุบัน .Net Framework เองก็ไม่มี Control ที่มาจัดการ Job Schedule ได้ จึงมีนักพัฒนาโปรแกรมหลายๆ คน พยายามสร้าง Control เข้ามาจัดการในส่วนของการใช้งาน Job Schedule ซึ่งมีอยู่ตัวหนึ่งที่อยากจะแนะนำ แล้วได้ทดลองใช้งานแล้วพบว่ามันสามารถทำงานได้เป็นอย่างดี

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

และมันสามารถที่จะนำมาใช้กับโปรเจคที่เขียนด้วย Windows Service ได้ด้วยเช่นเดียวกัน

Windows Service Job Schedule

ขั้นแรกให้ทำการ Add Reference ไฟล์ ScheduleTimer.dll เข้ามาใน Project

Windows Service Job Schedule

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

Windows Service Job Schedule

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








Windows Service Job Schedule

ScheduleTimer เข้ามาในโปรเจค จากนั้นเขียน Code ต่างๆ ดัวนี้

C#
        ScheduleTimer Timer = new ScheduleTimer();
        protected override void OnStart(string[] args)
        {
            string strPath = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";
            System.IO.File.AppendAllLines(strPath, new[] { "Starting time : " + DateTime.Now.ToString() });
            Timer.Elapsed += new ScheduledEventHandler(timer_Elapsed);
            Timer.AddEvent(new ScheduledTime("Daily", "23:33"));
            Timer.Start();
        }

        private void timer_Elapsed(object sender, ScheduledEventArgs e)
        {
            string strPath = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";
            System.IO.File.AppendAllLines(strPath, new[] { "..calling time : " + DateTime.Now.ToString() });
        }

        protected override void OnStop()
        {
            string strPath = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";
            System.IO.File.AppendAllLines(strPath, new[] { "Stop time : " + DateTime.Now.ToString() });
            Timer.Stop();
        }


VB.Net
    Private Timer As New ScheduleTimer()
    Protected Overrides Sub OnStart(ByVal args() As String)
        Dim strPath As String = AppDomain.CurrentDomain.BaseDirectory + "Log.txt"
        Dim lines() As String = {"Starting time : " + DateTime.Now.ToString()}
        System.IO.File.AppendAllLines(strPath, lines)

        AddHandler Timer.Elapsed, New ScheduledEventHandler(AddressOf timer_Elapsed)
        Timer.AddEvent(New ScheduledTime("Daily", "23:33"))
        Timer.Start()
    End Sub

    Private Sub timer_Elapsed(sender As Object, e As ScheduledEventArgs)
        Dim strPath As String = AppDomain.CurrentDomain.BaseDirectory + "Log.txt"
        Dim lines() As String = {"..calling time :" + DateTime.Now.ToString()}
        System.IO.File.AppendAllLines(strPath, lines)
    End Sub

    Protected Overrides Sub OnStop()
        Dim strPath As String = AppDomain.CurrentDomain.BaseDirectory + "Log.txt"
        Dim lines() As String = {"Stop time : " + DateTime.Now.ToString()}
        System.IO.File.AppendAllLines(strPath, lines)
    End Sub
จาก Code ตั้งให้ทำงานเวลา 23:30 และเขียนข้อความ calling time....

การ Install/Uninstall โปรแกรม Windows Service ที่ได้จาก Visual Studio (VB.Net,C#)

ทดสอบการทำงาน

Windows Service Job Schedule

ตอนที่นี้เวลา 23:30

Windows Service Job Schedule

แต่ Schedule ตั้งให้เวลาทำงาน 23:30

Windows Service Job Schedule

หลังจากที่ Install เรียบร้อยให้ทำการ Start Service

Windows Service Job Schedule

จะเห็นว่า Log.txt ถูกสร้างจาก OnStart และเขียนข้อความลงเข้าไป แต่ยังไม่ถึงเวลาที่ Job Schedule ทำงาน และ จะเขียนข้อความ calling time... เพิ่มเข้าไป

Windows Service Job Schedule

ถึงแล้ว 23:33

Windows Service Job Schedule

จะเห็นว่า Job Schedule ทำงานตอนเวลา 23:33 และเขียนข้อความเพิ่มลงไปใน Log.txt








   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2017-04-19 15:36:36 / 2017-04-20 15:02:21
  Download : No files
 Sponsored Links / Related

 
ตอนที่ 1 : รู้จัก Windows Service การสร้าง Application ให้รันบน Windows (VB.Net,C#)
Rating :

 
ตอนที่ 2 : การ Install/Uninstall โปรแกรม Windows Service ที่ได้จาก Visual Studio (VB.Net,C#)
Rating :

 
ตอนที่ 3 : การใช้ Timer บน Windows Service เพื่อกำหนดให้เวลา Services ทำงาน (VB.Net,C#)
Rating :

 
ตอนที่ 5 : การทำ Windows Service แบบ Automatic Start หลังจาก Reboot/Install (VB.Net,C#)
Rating :

 
ตอนที่ 6 : การทำ Windows Service ตั้งเวลา Schedule เปิดและรันโปรแกรมอื่นๆ (VB.Net,C#)
Rating :

 
ตอนที่ 7 : ตัวอย่าง Windows Service การเชื่อมต่อกับ Database SQL Server (VB.Net,C#)
Rating :

 
ตอนที่ 8 : การทำ Package และ Install Setup โปรแกรม Windows Service (VB.Net,C#)
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 01
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 อัตราราคา คลิกที่นี่