Winforms中的Windows服务 [英] windows service in winforms

查看:66
本文介绍了Winforms中的Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows服务.我的服务也创建了,它也位于服务中.很好,但是运行Windows服务时出现此错误无法从命令行或调试器启动服务.必须安装WINDOWS服务(使用installutil.exe),然后从服务器资源管理器启动.... ."

我的服务名是sai.我的应用程序名称是myapplication

在vs命令提示符下,我正在使用installutil myapplication.exe
bt仍然无法正常工作,我遇到了以上错误...我该怎么办.

i am using windows services . my service also created which is located at services also. it''s fine but i am getting this error when running my windows service "Can not start service from the command line or debugger. a WINDOWS SERVICE MUST BE INSTALLED(using installutil.exe) and then start with server explorer..... "

my service name is sai. my application name is myapplication

In vs command prompt i am using installutil myapplication.exe
bt also not working still i am getting above error... what can i do.

推荐答案

首先,可以将服务应用程序放置在任何有效位置.其次,它不能是在WinForms中"(顺便问一下,这是什么?).它不适用于是否具有Forms的交互式运行.

安装或删除服务仅是对系统注册表的修改.

现在,您需要使用System.Configuration.Install并以某种方式配置其属性,以便实用程序Installutil.exe可以将其用于安装,请参见http://msdn.microsoft.com/en-us/library/50614e95%28v=VS.100%29.aspx [ ^ ].使用这种安装方式不是很简单,并且我想说是违反直觉的.您将需要阅读大多数从参考页面开始的帮助页面,以了解如何做.作为奖励,您可以获得一个可以在双模式下工作的服务应用程序:作为服务应用程序(如果由服务控制器启动),或者当您从命令行管理程序简单运行时可以交互地进行操作.为什么与安装有关?因为在交互模式下,您可以实现自我安装程序.提示:要从服务模式告诉交互模式,请使用静态布尔属性System.Environment.UserInteractive,请参见 http://msdn.microsoft.com/en-us/library/system.serviceprocess .servicecontroller.aspx [ ^ ].

要启用服务的安装,服务应用程序应实现类System.ServiceProcess.ServiceProcessInstallerSystem.ServiceProcess.ServiceInstaller,请参阅帮助MSDN页面和代码示例:
http://msdn.microsoft.com/en-us/library/system.serviceprocess. serviceprocessinstaller.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.serviceprocess. serviceinstaller.aspx [ ^ ].

只有这样做,您才能使用installutil.exe.如果要在应用程序中执行安装/卸载,则应使用其他类System.Configuration.Install.AssemblyInstaller,请参见
First of all, a service application can be placed in any valid location. Second, it cannot be "in WinForms" (what is that, by the way?). It is not intended for interactive run, with Forms or not.

Installing or removing the service is merely a modification of system Registry.

Now, you need to use System.Configuration.Install and configure its property in a certain way so the utility Installutil.exe could use it for installation, see http://msdn.microsoft.com/en-us/library/50614e95%28v=VS.100%29.aspx[^]. Using this way of installation is not very simple and, I would say, counter-intuitive. You will need to read most of the help pages starting the referenced page to understand how to do it. As a reward, you can get a service application which can work in dual mode: as a service application if started by a Service Controller or interactively when you simply run it from the Shell. Why it is related to installation? Because in interactive mode you can implement self-installer. A hint: to tell interactive mode from a service mode, use the static Boolean property System.Environment.UserInteractive, see http://msdn.microsoft.com/en-us/library/system.environment.userinteractive.aspx[^]. You can also implement starting/restarting/stopping the service (which can run as another instance of the same application) using the API to the Service Controller. See http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx[^].

To enable installation of the service, the service application should implement the classes System.ServiceProcess.ServiceProcessInstaller and System.ServiceProcess.ServiceInstaller, see help MSDN pages and code samples:
http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceprocessinstaller.aspx[^],
http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.aspx[^].

Only if you do this, you can use installutil.exe. If you want to perform the installation/uninstallation in your application, you should use different class, System.Configuration.Install.AssemblyInstaller, see http://msdn.microsoft.com/en-us/library/system.configuration.install.assemblyinstaller.aspx[^]. It also required that a server application implements ServiceProcessInstaller and ServiceInstaller, as shown in the above paragraph.

—SA


除非在其中实现了main()函数,否则无法从命令行执行Windows服务.

服务要求您通过installutil.exe /i yourfilename.exe安装它们,然后使用services.msc服务启动器启动它们.
You cannot execute a windows service from the command line unless you have implemented a main() function in it.

Services require you to install them via installutil.exe /i yourfilename.exe and then start them with the services.msc service starter.


hai frinds这是解决方案..


使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Diagnostics;
使用System.Linq;
使用System.ServiceProcess;
使用System.Text;
使用System.IO;
使用System.Windows.Forms;

命名空间WindowsService2
{
公共局部类Service1:ServiceBase
{
System.Timers.Timer t;
公共Service1()
{
InitializeComponent();
}

受保护的重写void OnStart(string [] args)
{
Display();
}

受保护的void CreateTimer()
{
t =新的System.Timers.Timer();
t.间隔=(10000)* 6; //在这里,我将其设置为60秒以调用deletefiles方法
t.Elapsed + =新的System.Timers.ElapsedEventHandler(t_Elapsed);
}
受保护的void t_Elapsed(对象发送者,System.Timers.ElapsedEventArgs e)
{
DeleteFiles();
}

受保护的重写void OnStop()
{

}

公共无效的Display()
{
CreateTimer();
t.Enabled = true;
t.Start();

}
受保护的void DeleteFiles()
{
字符串[]文件= Directory.GetFiles(@您的目录路径");
for(int i = 0; i< files.length; i ++)
{
File.Delete(Files [i]);
}
}
}
}
hai frinds this is the solution..


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace WindowsService2
{
public partial class Service1 : ServiceBase
{
System.Timers.Timer t;
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
Display();
}

protected void CreateTimer()
{
t = new System.Timers.Timer();
t.Interval = (10000) * 6; // here i will set it to 60 secs to call deletefiles method
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
}
protected void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
DeleteFiles();
}

protected override void OnStop()
{

}

public void Display()
{
CreateTimer();
t.Enabled = true;
t.Start();

}
protected void DeleteFiles()
{
string[] Files = Directory.GetFiles(@"YOUR DIRECTORYPATH");
for (int i = 0; i < files.length; i++)
{
File.Delete(Files[i]);
}
}
}
}


这篇关于Winforms中的Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆