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



Clound SSD Virtual Server

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

การทำ Windows Service ตั้งเวลา Schedule เปิดและรันโปรแกรมอื่นๆ (VB.Net,C#) ในตัวอย่างนี้จะเป็นวิธีการเขียน Windows Service ทำการเรียกหรือเปิดโปรแกรมอื่นๆ หรือ Start Process/Execute ให้ทำงาน สามารถเปิดไฟล์โปรแกรมอื่นๆ ได้เช่น exe, bat แต่การทำงานของมันจะเป็นแบบ Background Process คือจะไม่แสดงหน้าจอ Interface ให้เห็น ส่วนวิธีการเปิดโปรแกรมนั้น สามารถที่จะให้ทำทันทีหลังจากที่ Start Services หรือจะใช้ Job Schedule มาช่วยก็ได้เช่นเดียวกัน

Windows Services Run Program


Start Process
Process.Start("notepad.exe");

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


วิธีการตั้ง Job Schedule เพื่อให้ Windows Service เรียก Start Process อื่นมาทำงาน

โดยจะทดสอบรันไฟล์ D:\myApp\myWinApplication.exe

C#
        ScheduleTimer Timer = new ScheduleTimer();
        protected override void OnStart(string[] args)
        {
            System.IO.File.AppendAllLines(strPath, new[] { "Starting time : " + DateTime.Now.ToString() });

            Timer.Elapsed += new ScheduledEventHandler(timer_Elapsed);
            Timer.AddEvent(new ScheduledTime("Daily", "09:55"));
            Timer.Start();
        }

        private void timer_Elapsed(object sender, ScheduledEventArgs e)
        {
            Process.Start(@"D:\myApp\myWinApplication.exe");
           
            System.IO.File.AppendAllLines(strPath, new[] { "..calling time : " + DateTime.Now.ToString() });
        }

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", "09:55"))
        Timer.Start()
    End Sub
    Private Sub timer_Elapsed(sender As Object, e As ScheduledEventArgs)

        Process.Start("D:\myApp\myWinApplication.exe")

        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

จาก Code นี้จะตั้งเวลาให้รันตอนเวลา 09:55

Windows Services Run Program

ตั้งเวลา 09:55 ให้รันไฟล์ D:\myApp\myWinApplication.exe

Windows Services Run Program

ตอนนี้เวลา 09:51

Windows Services Run Program

หลังจากที่ Services เริ่มทำงาน ในเบื้องต้น OnStart ทำงานและเขียน Log เวลาเพิ่มเข้าไป

Windows Services Run Program

เมือ่ถึงเวลา 09:55 ซึ่งเป็นเวลาที่ Job Schedule เริ่มทำงาน

Windows Services Run Program

จะมีการเขียน Log เพิ่มว่าเป็น calling time ซึ่งเป็นเวลาที่ตั้ง Job Schedule ไว้

Windows Services Run Program

เมื่อเปิด Task Manager ก็จะเห็นว่า Process ของ myWinApplication.exe ถูกเปิดและกำลังทำงาน

Note!! ในการใช้ Windows Service ทำการรันโปรแกรมอื่นๆ อาจจะต้องดูเรื่องสิทธิ์การเข้าถึงและสิทธิ์การรันโปรแกรมด้วย






   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2017-04-19 15:37:00 / 2017-04-20 17:21:48
  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 :

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

 
ตอนที่ 5 : การทำ Windows Service แบบ Automatic Start หลังจาก Reboot/Install (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 อัตราราคา คลิกที่นี่