2014-04-03 18 views
-1

在Windows Server 2003和Windows XP中,以自動(延遲)模式啓動Windows服務的功能不可用,因此我需要使用代碼進行該模擬,但是當啓動該服務時,會顯示一條消息,表明我需要等待cuz是代碼中的一個Timer,並且這需要完成啓動。但是現在我得到這個錯誤「本地計算機上的PcRegister服務服務啓動並停止,如果某些服務沒有任何工作要做,則會自動停止,例如性能日誌和警報服務」...請添加幫助以添加定時器。爲什麼我的服務不重新啓動並顯示錯誤(附)?

class WinService : System.ServiceProcess.ServiceBase` 
{ 
    static void Main() 
    { 
     ServiceBase[] ServicesToRun; 
     ServicesToRun = new ServiceBase[] { new WinService() }; 
     ServiceBase.Run(ServicesToRun); 
    } 


    protected override void OnStart(string[] args) 
    { 
     InitializeComponent(); 
     PcRegisterService(); 
    } 


    protected override void OnStop() 
    { 

    } 


    public void InitializeComponent() 
    { 
     this.ServiceName = "WinService"; 
     RestartService("WinService", 600000); 
    } 


    public static void RestartService(string serviceName, int timeoutMilliseconds) 
    { 
     ServiceController service = new ServiceController(serviceName); 

     try 
     { 
      int millisec1 = Environment.TickCount; 
      TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); 

      service.Stop(); 
      service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); 

      // count the rest of the timeout 
      int millisec2 = Environment.TickCount; 
      timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1)); 

      service.Start(); 
      service.WaitForStatus(ServiceControllerStatus.Running, timeout); 
     } 

     catch 
     { 
      // ... 
     } 

    } 


    public static void PcRegisterService() 
    { 
    } 


    public static class PerformanceInfo 
    { 
     [DllImport("psapi.dll", SetLastError = true)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     public static extern bool GetPerformanceInfo([Out] out PerformanceInformation PerformanceInformation, [In] int Size); 

     [StructLayout(LayoutKind.Sequential)] 
     public struct PerformanceInformation 
     { 
      public int Size; 
      public IntPtr CommitTotal; 
      public IntPtr CommitLimit; 
      public IntPtr CommitPeak; 
      public IntPtr PhysicalTotal; 
      public IntPtr PhysicalAvailable; 
      public IntPtr SystemCache; 
      public IntPtr KernelTotal; 
      public IntPtr KernelPaged; 
      public IntPtr KernelNonPaged; 
      public IntPtr PageSize; 
      public int HandlesCount; 
      public int ProcessCount; 
      public int ThreadCount; 
     } 

     public static Int64 GetTotalMemoryInMiB() 
     { 
      PerformanceInformation pi = new PerformanceInformation(); 
      if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) 
      { 
       return Convert.ToInt64((pi.PhysicalTotal.ToInt64() * pi.PageSize.ToInt64()/1048576)); 
      } 
      else 
      { 
       return -1; 
      } 

     } 
    } 
} 

回答

1

因爲 - 我會說 - 你吹的RestartServicve方法中的一些東西(有例外)。代碼沒有意義。就像沒有。根本不需要重新啓動服務。這是現在如何服務的工作。回到閱讀文檔。

+0

Im sorry,Im new in the program of programmation,在工作中都告訴我說Google是我最好的朋友......服務中的代碼太長了,原因是我不包括它.. 。我需要服務等待開始在最後cuz與另一個服務器建立網絡連接,所以如果不是網絡連接信息不能發送 –

+0

我有6個月學習如何編程jajaja –

+0

世界充滿了人withuit知識。忘記谷歌。閱讀文檔,學習語言。得到一兩本好書。不要尋找單一的解決方案,瞭解基礎知識。 – TomTom

相關問題