The problem in the top is that the Tick method should run all the time the service is running, and the service stops after the first execution, why is it tau? (

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.Net; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; using System.IO; using System.Threading; namespace WindowsService4 { public partial class Service1 : ServiceBase { [DllImport("advapi32.dll", SetLastError = true)] private static extern bool SetServiceStatus(System.IntPtr handle, ref ServiceStatus serviceStatus); public Service1() { } protected override void OnStart(string[] args) { Thread myThread = new Thread(Tick); //Создаем новый объект потока (Thread) myThread.Start(); // Update the service state to Start Pending. ServiceStatus serviceStatus = new ServiceStatus(); serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING; serviceStatus.dwWaitHint = 100000; SetServiceStatus(this.ServiceHandle, ref serviceStatus); serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING; SetServiceStatus(this.ServiceHandle, ref serviceStatus); } protected override void OnStop() { } private void Tick() { while (true) { Thread.Sleep(60000); String host = System.Net.Dns.GetHostName(); logg("Имя хоста: " + host); System.Net.IPAddress ip = System.Net.Dns.GetHostByName(host).AddressList[0]; string bufuerip = ip.ToString(); logg("IP адресс хоста " + bufuerip); string[] words = bufuerip.Split('.'); string podset = words[2]; logg("Разбитый ip адрес " + words[0] + " " + words[1] + " " + words[2] + " " + words[3]); string uri = "http://***************/indata.php?nt=" + podset; WebRequest webr = WebRequest.Create(uri); logg("Ответ от сайта: " + (HttpWebResponse)webr.GetResponse()); HttpWebResponse resp = null; resp.Close(); logg("Закрываем поток"); } } private void logg(string v) { File.WriteAllText(@"D:\logistic.log", v); } public enum ServiceState { SERVICE_STOPPED = 0x00000001, SERVICE_START_PENDING = 0x00000002, SERVICE_STOP_PENDING = 0x00000003, SERVICE_RUNNING = 0x00000004, SERVICE_CONTINUE_PENDING = 0x00000005, SERVICE_PAUSE_PENDING = 0x00000006, SERVICE_PAUSED = 0x00000007, } [StructLayout(LayoutKind.Sequential)] public struct ServiceStatus { public int dwServiceType; public ServiceState dwCurrentState; public int dwControlsAccepted; public int dwWin32ExitCode; public int dwServiceSpecificExitCode; public int dwCheckPoint; public int dwWaitHint; }; } } 
  • one
    This is HttpWebResponse resp = null; will lead to an error in the next resp.Close(); NullReferenceException - Bulson pm
  • 2
    And a couple of notes: 1) instead of Thread it is better to use Task , if only because it is possible to intercept errors normally and cancel tasks at the right moment, 2) for logging it is better to use Trace and not, the disgrace that you composed. 3) for the application working with the network, you must use try / catch and seemingly intercept UnhandledException as it can be done here . - Bulson

0