-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;
}
}
}
}
Im sorry,Im new in the program of programmation,在工作中都告诉我说Google是我最好的朋友......服务中的代码太长了,原因是我不包括它.. 。我需要服务等待开始在最后cuz与另一个服务器建立网络连接,所以如果不是网络连接信息不能发送 –
我有6个月学习如何编程jajaja –
世界充满了人withuit知识。忘记谷歌。阅读文档,学习语言。得到一两本好书。不要寻找单一的解决方案,了解基础知识。 – TomTom