我如何以编程方式将参数传递给服务的 OnStart 方法,我还需要将其传播到服务内计时器的 Elapsed 事件?
最佳答案
在最简单的层面上:当您调用 ServiceBase.Run
时,您可以为其提供要执行的服务实例。只需将其声明为您服务的公共(public)属性,并在调用 Run
之前分配:
Service1 myService = new Service1();
myService.SomeProp = 1;
ServiceBase.Run(myService);
然后在你的服务中读取SomeProp
:
public int SomeProp { get; set;}
protected override void OnStart(string[] args)
{
int prop = SomeProp;
}
您也可以使用服务参数,但那是来自外部调用者(服务注册)——而不是以编程方式(根据问题)。
关于c# - 如何以编程方式将参数传递给 Windows 服务的 OnStart 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/584913/