为什么我安装的Windows服务不起作用? [英] Why my installed windows service isn't working ?

查看:85
本文介绍了为什么我安装的Windows服务不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了一个Windows服务来运行每分钟并将驱动器的文件夹中的文件移动到另一个驱动器的文件夹中?我正在使用VS 2005和c#2.0。该服务可在services.msc中看到。但在启动服务时,它会自动启动和停止。如果他们有工作要做,服务会自动停止。以下是代码。



  public  Service1()
{
InitializeComponent();
}

受保护 覆盖 void OnStart( string [] args)
{

// TODO:在此处添加代码以启动服务。
this .objTimer.Elapsed + = new ElapsedEventHandler(OnElaspedEvent);

// 60000毫秒= 1分钟。
int Interval_Minute = Convert.ToInt32(ConfigurationManager.AppSettings [ INTERVAL ]的ToString());
objTimer.Interval = POLL_INTERVAL * Interval_Minute;

// 启用服务。
objTimer.Enabled = true ;
}

受保护 覆盖 void OnStop()
{
// TODO:添加代码在这里执行任何必要的拆卸以停止服务。
objTimer.Enabled = false ;
}
私有 void OnElaspedEvent( object source,ElapsedEventArgs e)
{
MoveFile();
}
私有 void MoveFile()
{
string sourcepath = ConfigurationManager.AppSettings [ SOURCE_PATH< /跨度>]的ToString()修剪()。;
string destinationpath = ConfigurationManager.AppSettings [ 。destination_path可]的ToString()修剪();
if (File.Exists(sourcepath))
File.Move(sourcepath,destinationpath);
// else
/ / MessageBox.Show(No File Exists);
// if(File.Exists(destinationpath))
// MessageBox.Show(文件已经处理完毕);






}

解决方案

您的代码似乎生成访问冲突异常,您的服务将停止..



1.您忘记管理服务中的例外情况。您应该以这样的方式管理它们,即使发生异常,您的服务仍将保持正常运行;您还应该在Windows事件日志中写入通知,至少是异常数据。



您可以找到有关异常管理的详细信息,包括用于将通知写入的一些util类Windows事件登录到我的下一篇文章: MVC基本站点:第2步 - 例外管理 [ ^ ]



2.要解决访问冲突异常,安装后,必须使用具有<的用户启动Windows服务code> rights 到包含您尝试管理的文件的主文件夹。



请注意,在管理员管理窗口中,当您启动服务时,您可以为计算机的现有用户提供凭据(用户名和密码)

I installed a windows service to run every minute and move the file in a folder of a drive to a folder of another drive? I am using VS 2005 and c#2.0. The service is seen in services.msc. But on starting the service it starts and stops automatically. Service Stops automatically if they have on work to do. Following is the code.

public Service1()
      {
          InitializeComponent();
      }

      protected override void OnStart(string[] args)
      {

          // TODO: Add code here to start your service.
          this.objTimer.Elapsed += new ElapsedEventHandler(OnElaspedEvent);

          //60000 milliseconds = 1 minute.
          int Interval_Minute = Convert.ToInt32(ConfigurationManager.AppSettings["INTERVAL"].ToString());
          objTimer.Interval = POLL_INTERVAL * Interval_Minute;

          //Enable the service.
          objTimer.Enabled = true;
      }

      protected override void OnStop()
      {
      // TODO: Add code here to perform any tear-down necessary to stop your service.
          objTimer.Enabled = false;
      }
      private void OnElaspedEvent(object source, ElapsedEventArgs e)
      {
          MoveFile();
      }
      private void MoveFile()
      {
          string sourcepath = ConfigurationManager.AppSettings["SOURCE_PATH"].ToString().Trim();
          string destinationpath = ConfigurationManager.AppSettings["DESTINATION_PATH"].ToString().Trim();
          if (File.Exists(sourcepath))
              File.Move(sourcepath, destinationpath);
          //else
          //    MessageBox.Show("No File Exists");
          // if (File.Exists(destinationpath))
          //     MessageBox.Show("File Already Processed");






      }

解决方案

It seems that your code generate access violation exception and your service will stop..

1.You forgot to manage the exceptions into your service. You should mange them in the way that even if exception will occur your service will remain up and running; and also your should write notification into the Windows Event Log, at least the exceptions data.

You could find details about exception management, including some util class for writing notifications into the Windows Event Log into my next article: MVC Basic Site: Step 2 - Exceptions Management[^]

2.To solve the access violation exception, after installation, you have to start your windows service by using a user that have rights to the main folder that contains the files that you are trying to manage.

Note that in the administrator management window, when you start a service, you have the possibility to provide the credentials (user name and password) for existing users of the computer.


这篇关于为什么我安装的Windows服务不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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