简体   繁体   English

TopShelf如何捕获诸如“安装”或“启动”之类的参数,而这些参数却被我的Main方法忽略了?

[英]How TopShelf can capture arguments like 'install' or 'start' which is ignored by my Main method?

I was checking the TopShelf as a beginner. 我正在以初学者的身份检查TopShelf。

I see that when you compile your console app you can install your service as: 我看到编译控制台应用程序时,可以将服务安装为:

MySampleWindowsService.exe install

and start: 然后开始:

MySampleWindowsService.exe start

What I do not understand is my console app does not do anything with those parameters. 我不明白我的控制台应用程序对这些参数没有任何作用。

Question :So how the TopShelf captures those 'install' or 'start' arguments? 问题 :那么TopShelf如何捕获那些“安装”或“启动”参数?

It probably uses Environment.CommandLine or Environment.GetCommandLineArgs . 它可能使用Environment.CommandLineEnvironment.GetCommandLineArgs

The command line of a process is stored in a special area of memory set aside when the process is created, which is how it's available at any time during the process's execution. 进程的命令行存储在创建进程时预留的特殊内存区域中,这是在进程执行期间随时可用的方式。 It can be retrieved by the native GetCommandLine function, which is the "official" way of getting command-line arguments in a Windows process, and is wrapped by the .NET Environment members. 可以通过本机的GetCommandLine函数进行检索,该函数是Windows进程中获取命令行参数的“官方”方法,并且由.NET Environment成员包装。

Passing the command line as parameters to the "main" function in many languages is a convenience provided by the language. 以多种语言将命令行作为参数传递给“ main”函数是该语言提供的一种便利。 The compiler generates startup code that calls GetCommandLine and passes the result as a parameter to main (or equivalent). 编译器生成启动代码,该启动代码调用GetCommandLine ,并将结果作为参数传递给main (或等效参数)。 Similarly, the return value (if any) from main is usually set as the process's exit code by compiler-generated process tear-down logic. 同样, main的返回值(如果有的话)通常由编译器生成的进程拆解逻辑设置为进程的退出代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM