Topshelf-根据自定义参数启动线程//********************下面是一段TopShelf代码***********************************//HostFactory.Run(hostConfigurator=>{hostConfigurator.AddCommandLineDefinition("department",f=>{department=f;});//定义新参数hostConfigurator.ApplyCommandLine();//应用它Helpers.LogFile("xxx","Gotdepartment:"+department);hostConfigurator.Service(serviceConfigurator=>{serviceConfigurator.ConstructUsing(()=>newMyService(department));//我们使用什么服务正在使用serviceConfigurator.WhenStarted(myService=>myService.Start());//启动时运行什么;//*********************为其他服务更改这些名称********************************************//stringd="CallForwardService_"+department;hostConfigurator.SetDisplayName(d);hostConfigurator.SetDescription("CallForwardusingTopshelf");hostConfigurator.SetServiceName(d);});...publicclassMyServicestringdepTask;publicMyService(stringd){//************************三个不同目的地的三个任务************************depTask=d;_taskL=newTask(Logistics);_taskP=newTask(Planners);_taskW=newTask(Workshop);Helpers.LogFile(depTask,"开始处理线程"+d);publicvoidStart(){if(depTask=="logistics"){_taskL.Start();Helpers.LogFile(depTask,"properthreadselected");}...Helpers.logfile只是写入一个文本文件你可以从上面得到我在代码中看到参数部门传递给MyService(stringd)。当我使用调试时,一切正常,即“-department:workshop”作为调试参数。但是当我尝试安装使用callforward.exeinstall-department:logistics的程序作为callforward.exeinstall-department:logistics服务,我在检查日志时创建了服务callforwardservice_logisticsbu,参数尚未传递给MyService。我究竟做错了什么?默认情况下,Topshelf似乎不支持在服务启动配置中添加自定义参数,安装后HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesMyService下的ImagePath值不包含其他参数-department:...可以继承默认的WindowsHostEnvironment重载Install方法,但是我觉得会在host配置代码中加入如下代码会更简单(可能更糟):以上是C#学习教程:Topshelf-Startingathreadbasedoncustomparameters分享所有内容,如果对大家有用并且需要了解更多C#学习教程,希望大家多多关注——//*********************下面是一段TopShelf代码*******************************//HostFactory.Run(hostConfigurator=>{...hc.AfterInstall(ihc=>{使用(RegistryKeysystem=Registry.LocalMachine.OpenSubKey("System"))使用(RegistryKeycurrentControlSet=system.OpenSubKey("CurrentControlSet"))使用(RegistryKeyservices=currentControlSet.OpenSubKey("Services"))使用(RegistryKeyservice=services.OpenSubKey(ihc.ServiceName,true)){constStringv="ImagePath";varimagePath=(字符串)服务。获取值(v);服务。SetValue(v,imagePath+String.Format("-department"{0}"",department));}});...}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
