Using. NET to solve the problem of Go Web service stop after server logoff

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Problem Description: After the server logs off, all running. exe files associated with the user are exited, which is why the. exe program for the GO website is closed

Workaround: Through the Windows service

There are many ways to make Windows services online, but not all EXE files can be serviced, and the. exe on the go build just can't! So, it can only be done in another way: using. NET to build a service project, In this service project to monitor the operation of the Go Web. exe program, if the go site's. exe program stops, then immediately start it in the Windows service, so even if the logoff is not afraid.

Details are as follows:

1) Go build exe file (my in D:\Go\src\myweb\myweb.exe)

2) Establish Windowsservice project (note version)

Core code:

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.diagnostics;using system.linq;using system.serviceprocess;using system.text;using System.Threading.Tasks;            Using System.timers;namespace bdiaservice{public partial class Bdia:servicebase {public Bdia () {        InitializeComponent (); }///<summary>//The function that is called when the service starts///</summary>//<param name= "args" >< /param> protected override void OnStart (string[] args) {Check2 ();//Start MyWeb application Tim            er t1 = new Timer (60000); T1. Elapsed + = new Elapsedeventhandler (check);//Check T1 once every 1 minutes.  Start ();        Start the timer. }///<summary>//When the service is stopped, close the MyWeb///</summary> protected override void on            Stop () {process[] myprocess = Process.getprocessesbyname ("MyWeb"); if (myprocess. LengtH > 0) {myprocess[0].                CloseMainWindow (); Myprocess[0].            Close (); }}///<summary>///Timer execution function///</summary>//<param name= "O" ><        /param>//<param name= "E" ></param> public void Check (Object o, Elapsedeventargs e) {process[] myprocess = Process.getprocessesbyname ("MyWeb");//Get the process with the name MyWeb if (myprocess. Length > 0)//If present, the description has been started {//off//myprocess[0].                CloseMainWindow (); Myprocess[0].            Close ();            } else//otherwise start Myweb.exe {Process.Start (@ "D:\Go\src\myweb\myweb.exe"); }} public void Check2 () {process[] myprocess = Process.getprocessesbyname ("MyWeb"            ); if (myprocess. Length > 0) {//myprocess[0].                CloseMainWindow (); // Myprocess[0].            Close ();            } else {Process.Start (@ "D:\Go\src\myweb\myweb.exe"); }        }    }}


3) Windowsservice Project installation Deployment (as long as the. NET is installed on the computer, the following code is generally available, here is my. exe file path):

C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe   D:\Go\src\myweb\myweb.exe



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.