Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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.

C#
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");






      }
Posted
Comments
Herman<T>.Instance 4-Jun-14 7:58am    
have you checked if you get any exceptions, by using a try catch and write the exception to a file?
johannesnestler 4-Jun-14 11:06am    
A guess: (but after years of experience) - permission problem during file handling - try to get the exact exception (as digimanus suggested).
ZurdoDev 5-Jun-14 21:23pm    
Look in the event viewer.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900