【发布时间】:2011-01-04 13:17:06
【问题描述】:
我已经构建了一个 Windows 服务,现在我希望它能够自动更新。我已经阅读了有关创建第二个服务来执行此操作或其他程序的信息,不能使用单击一个,myBuild 呢?有人知道吗?什么是最好的方法?我可以只更改程序集吗?
【问题讨论】:
标签: c# .net windows-services auto-update
我已经构建了一个 Windows 服务,现在我希望它能够自动更新。我已经阅读了有关创建第二个服务来执行此操作或其他程序的信息,不能使用单击一个,myBuild 呢?有人知道吗?什么是最好的方法?我可以只更改程序集吗?
【问题讨论】:
标签: c# .net windows-services auto-update
如果您希望在执行更新时运行服务,这是我之前为实现此目的所做的:
【讨论】:
要重启你的服务
System.Diagnostics.Process.Start
(System.Reflection.Assembly.GetEntryAssembly().Location)
那就为你效劳
private const string _mutexId = "MyUniqueId";
private static Mutex _mutex;
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
try
{
bool alreadyRunning = false;
try
{
Mutex.OpenExisting(_mutexId);
alreadyRunning = true;
}
catch (WaitHandleCannotBeOpenedException)
{
alreadyRunning = false;
}
catch
{
alreadyRunning = true;
}
if (alreadyRunning)
{
using (ServiceController sc = new ServiceController("MyServiceName"))
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 120));
sc.Start();
}
return;
}
}
catch
{
}
_mutex = new Mutex(true, _mutexId);
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
// Load the service into memory.
ServiceBase.Run(ServicesToRun);
_mutex.Close();
}
【讨论】:
您可以修改您的 Windows 服务,使其只是您的主应用程序的运行程序,并具有更新您的主应用程序的功能。
所以你会有:
Service.exe:运行 Application.exe,监控远程位置以获取 Application.exe 的更新。向 Application.exe 发送启动/停止事件
Application.exe:以前是您的 Service.exe。接收开始/停止事件。
【讨论】: