. Net to create, uninstall, and debug Windows Services

Source: Internet
Author: User

Because the project involves interface data transfer with other systems, the interface data is synchronized by creating a new Windows service. The content is very simple. The reason why I wrote it down is to deepen my influence and keep a backup.
Let's take the above steps directly:
First, we create a Windows system named myservice. Program . Then we changed service1 to myservice.
Second, declare a thread, private thread thd; and add the following test in the onstart method of myservice. Code :
If (THD = NULL | thd. threadstate! = System. Threading. threadstate. Running)
{
Thd = new thread (New threadstart (runapp ));
Thd. Start ();
}
Third, add the following Code. The Code is written to the log file in the search path devenv.exe.
Public void runapp ()
{
While (true)
{
Int I = 0;
Foreach (system. Diagnostics. PROCESS p in system. Diagnostics. process. getprocesses ())
{
If (P. processname = "devenv ")
{
Loglocation = system. Windows. Forms. application. startuppath + "\" + "log ";
Logmsg ("test", "Find progress devenv.exe ");
}
}
}
Thread. Sleep (1000 );
}
Public static string loglocation = "";
Public static bool logmsg (string ID, datetime logdate, string MSG)
{
Try
{
If (ID = NULL | ID. Trim () = "")
Return false;
If (loglocation = "")
Return false;

string Path = loglocation + "\" + ID + "\" + logdate. tostring ("yyyymm");
string file = path + "\" + logdate. tostring ("yyyymmdd") + ". TXT ";
If (! System. Io. Directory. exists (PATH)
system. Io. Directory. createdirectory (PATH);
If (! System. Io. file. exists (File)
system. Io. file. Create (file). Close ();

System. Io. streamwriter Sw = new system. Io. streamwriter (file, true );
Sw. writeline (formatstr (MSG ));
Sw. Close ();
Return true;
}
Catch (exception ex)
{
Return false;
}
}
Private Static string formatstr (string MSG)
{
Return "[" + datetime. Now. tostring ("yyyy-mm-dd hh: mm: SS") + "]" + MSG;
}
Public static bool logmsg (string ID, string MSG)
{
Return logmsg (ID, datetime. Now, MSG );
}
Fourth, write the following code in the onstop method:
If (THD! = NULL)
{
Thd. Abort ();
Thd = NULL;
}
Our service code has been written, and the next task is to register the service.
Fifth, click myservice. CD to view the designer, and right-click it to add the installer. After you click it, The serviceprocessinstaller1 and serviceinstaller1 components are displayed. Change servicename of serviceinstaller1 to myservice. There are two points to note here. one is starttype. There are three options: automatic, manual, and disabled. if You Want To Enable Automatic startup, select automatic once. another attribute that needs to be noted is account. Select User. Generally, select local system .. In the program. CS file, there is a segment of code used to start the service:
Static void main ()
{
Servicebase [] servicestorun;

// Multiple user services can be run in the same process. To
// Add another service to this process. Please change the downstream
// Create another service object. For example,
//
// Servicestorun = new servicebase [] {New service1 (), new myseconduserservice ()};
//
Servicestorun = new servicebase [] {New myservice ()};

Servicebase. Run (servicestorun );
}

Generate the solution.
sixth, install the service, open the Visual Studio 2005 command prompt, switch to the bin \ DEBUG directory of your program, enter installutil myservice.exe, and then prompt that the installation is successful. We can go to our service to find the service that we just registered, and then start the process of myservice.exe. Go to the \ bin \ DEBUG directory and find whether there is a log file containing today's text files.
seventh, uninstall the service, switch to the bin \ DEBUG directory of your program, and enter installutil/u myservice.exe.
eighth, debug the service, start the Microsoft Visual Studio environment, and at the same time start the wizard-pilot additional processes. Click to display all user processes, find the process named myservice.exe, and click Add-on. Then we can set breakpoint debugging in our solution. Over! Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.