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 > ตอนที่ 5 : การทำ Windows Service แบบ Automatic Start หลังจาก Reboot/Install (VB.Net,C#)



Clound SSD Virtual Server

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

การทำ Windows Service แบบ Automatic Start หลังจาก Reboot/Install (VB.Net,C#) จากบทความก่อนหน้านี้เราได้เรียนรู้วิธีการ Install โปรแกรม Windows Service ลงใน Windows แล้ว แต่ถ้าสังเกตุจเห็นว่า Windows Service ทีไ่ด้ถูกติดตั้งนั้นจะไม่ทำการ Start แบบ Automatic และสถานะการ Start ยังเป็น Manual ซึ่งโดยปกติแล้วโปรแกรมประเภท Windows Service หลังจากที่ติดตั้งแล้ว มันควรจะทำการ Start ตัวเองอัตโนมัติ รวทั้งเมื่อ Boot หรือ Reboot เครื่องก็ควรจะทำการ Start แบบ Automatic ด้วยเช่นเดียวกัน

Windows Service Automatic Start


การทำให้โปรแกรม Start เองแบบ Automatic นั้นทำได้ง่ายมากๆ เพียงกำหนด StartType เป็นแบบ Automatic (ตัวนี้จะมีผลตอนที่ Boot/Reboot) และสร้าง Event ของ AfterInstall แล้วกำหนดให้ทำการ Start ตัว Service ขึ้นมาหลังจากการติดตั้ง

Windows Service Automatic Start Reboot Install

จะเห็นว่าหลังจากที่ติดตั้ง Service ยังไม่ Start และยังเป็นแบบ Manual อยู่

Windows Service Automatic Start Reboot Install

ซึ่งใน Services Control Manager ก็สามารถกำหนดค่านี้ได้แบบ Manual เช่นเดียวกัน

วิธีการทำ Automatic Start หลังจากที่ Install และ Boot/Reboot

Windows Service Automatic Start Reboot Install

เปิดไฟล์ ProjectInstaller

Windows Service Automatic Start Reboot Install

ที่ serviceInstaller1 กำหนด StartType = Automatic

Windows Service Automatic Start Reboot Install

สร้าง Event ในส่วนของ AfterInstall

Windows Service Automatic Start Reboot Install

Event ทีไ่ด้ จากนั้นเพิ่มคำสั่งนี้เข้าไป

C#
        private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {
            using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController(serviceInstaller1.ServiceName))
            {
                serviceController.Start();
            }
        }

VB.Net
    Private Sub ServiceInstaller1_AfterInstall(sender As Object, e As InstallEventArgs) Handles ServiceInstaller1.AfterInstall
        Using serviceController As New System.ServiceProcess.ServiceController(ServiceInstaller1.ServiceName)
            serviceController.Start()
        End Using
    End Sub

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


Windows Service Automatic Start Reboot Install

จากนั้นทำการ Install ใหม่อีกครั้ง

Windows Service Automatic Start Reboot Install

จะเห็นว่า Service ได้ทำการ Start อัตโนมัติ และ StartType เป็นแบบ Automatic

Windows Service Automatic Start Reboot Install

ซึ่งใน Code นี้จะเป็นการสร้าง Log.txt ซึ่งจะถูกทำงานอัตโนมัติ






   
Share


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


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


   


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

 
ตอนที่ 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 อัตราราคา คลิกที่นี่