Sao không tự làm lại thử rồi hướng dẫn bằng tiếng Việt luôn vậy neverland87?. Mình thấy đa số toàn bài viết các bạn bê nguyên xi từ chổ khác không àh. Sao không làm cái gì đó có dấu ấn của CViet nhỉ?!.
Creating a Windows Service in C#
By Mahesh Chand January 23, 2001
NOTE: The project name in this sample code is mcWebService which is a spelling mistake. I meant to put mcWinService. And now I don't want to change all the screen shots and code all over again. I hope it won't confuse you.
Ok, its time for one more tutorial. This times pick is Windows Services. Creating Windows Services is not a big deal using C# and Visual Studio. Just follow few simple steps and you are all set to run and test your first Windows Service.
Windows Services is new name for NT Services you used to develop in previous versions of Visual Studio. This tutorial walks you through how to create and use your Windows Services. This Service writes some text to a text file when stop and start the service. The base idea is taken from MSDN but its more elaborated. You can modify it according to your needs.
Step 1. Create Skeleton of the Service
To create a new Window Service, pick Windows Service option from your Visual C# Projects, give your service a name, and click OK.
The result look like this. The Wizard adds WebService1.cs class to your project.
Set your ServiceName to your own name so it would be easier to recognize your service during testing OR you can set this property programmatically using this line this.ServiceName = "mcWinService";
This is the name you will be looking for later.
The default code of WebService1.cs added by the Wizard looks like here
Visual C# Code:
namespace mcWebService { { /// <summary> /// Required designer variable. /// </summary> { // This call is required by the WinForms Component Designer. InitializeComponent(); // TODO: Add any initialization after the InitComponent call } // The main entry point for the process { System.ServiceProcess.ServiceBase[] ServicesToRun; // More than one user Service may run within the same process. To add // another service to this process, change the following line to // // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new WinService1(), new ySecondUserService()}; // System.ServiceProcess.ServiceBase.Run(ServicesToRun); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> { } /// <summary> /// Set things in motion so your service can do its work. /// </summary> { // TODO: Add code here to start your service. } /// <summary> /// Stop this service. /// </summary> { // TODO: Add code here to perform any tear-down necessary to stop your service. } } }
Step 2. Add functionality to your service
As you saw WebService1.cs, there are two overridden functions OnStart and OnStop. The OnStart function executes when you start your service and the OnStop function gets execute when you stop a service. I write some text to a text file when you start and stop the service.
Visual C# Code:
{ FileMode.OpenOrCreate, FileAccess.Write); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine(" mcWindowsService: Service Started \n"); m_streamWriter.Flush(); m_streamWriter.Close(); } /// <summary> /// Stop this service. /// </summary> { FileMode.OpenOrCreate, FileAccess.Write); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine(" mcWindowsService: Service Stopped \n"); m_streamWriter.Flush(); m_streamWriter.Close(); }
Step 3: Install and Run the Service
Build of this application makes one exe, mcWinService.exe. You need to call installutil to
egister this service from command line.
installutil C:\mcWebService\bin\Debug\mcWebService.exe
You use /u option to uninstall the service.
installutil /u C:\mcWebService\bin\Debug\mcWebService.exe
Run the application
Step 4: Start and Stop the Service
You need to go to the Computer Management to Start to start and stop the service. You can use Manage menu item by right clicking on My Computer.
Under Services and Applications, you will see the service mcWinService. Start and Stop menu item starts and stops the service.
Step 5: Test the Service
Go to your temp directory and see if text file is there with contents or not.
That's it.
Sao không tự làm lại thử rồi hướng dẫn bằng tiếng Việt luôn vậy neverland87?. Mình thấy đa số toàn bài viết các bạn bê nguyên xi từ chổ khác không àh. Sao không làm cái gì đó có dấu ấn của CViet nhỉ?!.
Đã được chỉnh sửa lần cuối bởi NoBi : 18-05-2007 lúc 11:08 PM. Lý do: Thêm
phải đấy, cũng nên có một chút gì đó "CViệt" chứ
Việt Nam quang vinh muôn năm!!!!!!!!!!!!
Cám ơn anh Nobi và bạn halh0000 đã góp ý. Tuy em là mod C# nhưng hiện trình độ còn kém lắm, chủ yếu mày mò c# trên mấy websites nước ngoài và không có năng khiếu viết bài nên đành post những gì người ta viết vào topic.
Hiện mình thấy box C# còn khá im ắng trong CV, rất mong nhận được những bài viết, câu hỏi, kinh nghiệm lập trình C# của mọi người.
acx nghĩ hầu hết đều đang mày mò trong cái quá trình học :| nên chưa có gì đặc sắc, chờ xong rồi tung ra sau :pSao không tự làm lại thử rồi hướng dẫn bằng tiếng Việt luôn vậy neverland87?. Mình thấy đa số toàn bài viết các bạn bê nguyên xi từ chổ khác không àh. Sao không làm cái gì đó có dấu ấn của CViet nhỉ?!.
acx nghĩ hầu hết đều đang mày mò trong cái quá trình học :| nên chưa có gì đặc sắc, chờ xong rồi tung ra sau :
acx àh, không nên nghĩ như thế chứ, chúng mình cùng đang học hỏi thì có gì phải nói ngay để anh em cùng giải quyết như thế mới tiến bộ nhanh được chớ
Việt Nam quang vinh muôn năm!!!!!!!!!!!!
các bác nên chăm chút bài viết của mình, post tiếng việt thì mới "ăn khách", đa số các thành viên (ở hầu hết các diễn đàn người việt) thường lười đọc tiêég anh lắm, nhất là dân lập trình
phải cố gắng thì mới phát triển CViet đc chứ
Em nghĩ là viết phần mềm ở việt nam ko bán được => ko ai tự viết cả. Em thấy mấy công ty lập trình cũng đi copy code về rồi khen họ viết hay chứ đâu có tự viết đâu. Nếu congdongcviet mà cũng copy code rồi thì dán hóa ra cũng là người việt nam)
Đó là do "cái lười" của dân VN mà ra đó. Chứ dân lập trình là phải biết đọc-viết tiếng Anh một cách thành thạo.
Thì bản chất chúng mình là người Việt Nam màNếu congdongcviet mà cũng copy code rồi thì dán hóa ra cũng là người việt nam![]()
Ở đây, mọi người nên hiểu là 1 bài viết chia sẻ cho việc học tập, chứ không phải là chuyện ăn cắp code kiếc gì cả.