-1
我使用c#创建了Windows窗体应用程序。现在我需要添加一个Windows服务以及这个应用程序。我添加了一个新的Windows服务并添加了安装程序。我创建了Windows安装程序并将其安装在PC中,但该服务无法正常工作。我是C#的新手。请帮助我将此服务添加到安装程序。Windows窗体应用程序中的Windows服务
我使用c#创建了Windows窗体应用程序。现在我需要添加一个Windows服务以及这个应用程序。我添加了一个新的Windows服务并添加了安装程序。我创建了Windows安装程序并将其安装在PC中,但该服务无法正常工作。我是C#的新手。请帮助我将此服务添加到安装程序。Windows窗体应用程序中的Windows服务
WinForms应用程序和Windows服务项目模板具有不同的引导代码(请参阅项目中的“Program.cs”文件)。
这一个从Windows窗体:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
这一个从Windows服务:
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
如果你想将这些类型的应用程序在一个单一的可执行文件合并,则需要修改引导代码一点:
// we need command line arguments in Main method
[STAThread]
static void Main(string[] args)
{
if (args.Length > 0 && args[0] == "service")
{
// runs service;
// generated bootstrap code was simplified a little
ServiceBase.Run(new[]
{
new Service1()
});
}
else
{
// runs GUI application
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
现在,安装服务时,需要设置命令用于运行可执行文件的行参数:myExe service
。
“但该服务无法正常工作” - 我们如何帮助您而不提供更多信息? –
你开始服务了吗?你怎么知道它不工作?我们需要的方式更多信息 – Pavenhimself
大家好,感谢您的回复,基本问题是我是C#的初学者。我结合了来自互联网的代码。我的要求是,当没有互联网时,数据将被存储在一个文件中。我正在使用Windows窗体处理这部分内容。现在我需要一个服务来检查互联网连接,当互联网可用时,它会自动将文件上传到网络服务。 –