Asp.net ends the server process

Source: Internet
Author: User

For a special reason, you need to restart a program on the server through the web page, that is, kill the program first, and then start again, because the permissions of Asp.net users are insufficient, if you directly kill the process, it will be wrong. I checked it online and found a better solution, so I used a very stupid method:

First, I wrote a service program. The function is to check the content in a file every second. If it is wait, nothing is done. If it is restart, restart the process. Write the service program and install it on the system.

// Program. CS

Using system;
Using system. Collections. Generic;
Using system. serviceprocess;
Using system. text;

Namespace CHED
{
Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
Static void main ()
{
Servicebase [] servicestorun;
Servicestorun = new servicebase []
{
New restart_serv_u ()
};
Servicebase. Run (servicestorun );
}
}
}

// Restart_serv_u.cs

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. serviceprocess;
Using system. text;
Using Microsoft. Win32;
Using system. IO;
Using system. Threading;
Using system. Windows. forms;

Namespace CHED
{
Public partial class restart_serv_u: servicebase
{
Public restart_serv_u ()
{
Initializecomponent ();
}

Protected override void onstart (string [] ARGs)
{
Thread chedthread = new thread (serverstart );
Chedthread. Start ();
}

Protected override void onstop ()
{
}

Private void serverstart ()
{
CHED lcysc = new CHED ();
Application. Run (lcysc );
}
}
}

// Restart_serv_u.designer.cs

Namespace CHED
{
Partial class restart_serv_u
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. icontainer components = NULL;

/// <Summary>
/// Clear all resources in use.
/// </Summary>
/// <Param name = "disposing"> If the managed resource should be released, the value is true; otherwise, the value is false. </Param>
Protected override void dispose (bool disposing)
{
If (disposing & (components! = NULL ))
{
Components. Dispose ();
}
Base. Dispose (disposing );
}

# Code generated by the region component designer

/// <Summary>
/// The designer supports the required methods-do not
/// Use the code editor to modify the content of this method.
/// </Summary>
Private void initializecomponent ()
{
Components = new system. componentmodel. Container ();
This. servicename = "service1 ";
}

# Endregion
}
}

// Ched. CS

Using system;
Using system. Windows. forms;
Using system. Threading;
Using Microsoft. Win32;
Using system. IO;

Namespace CHED
{
Public partial class CHED: applicationcontext
{
Public CHED ()
{
Watcher = new system. Threading. Timer (New timercallback (watchercallback), null, 1000,100 0 );
}

Private system. Threading. Timer watcher;

Private void watchercallback (Object O)
{
Try
{
String text = file. readalltext (@ "D:/webroot/CEDC/manage/admin/CHED/reset. CHED ");
If (text = "restart ")
{
System. Diagnostics. Process [] pros = system. Diagnostics. process. getprocessesbyname ("Serv-U ");
If (pros. length> 0)
Pros [0]. Kill ();
File. writealltext (@ "D:/webroot/CEDC/manage/admin/CHED/reset. CHED", "wait ");
}
}
Catch {}
}
}
}

Add a reference to system. Windows. forms.

Finally, in the webpage button event, write the file content as restart.

Of course, you can end multiple other processes in the same way. For example, you can write the name of the process to be completed in the text. When the service program on the server finds out, kill the process.

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.