D
DaveF
I am trying to write a service to fire an ftp object off. I want to wait 2
minutes and then download the files. Then repeat every 2 minutes. The files
just seem to stop downloading. Does that timer go off every 2 minutes if the
files are done or not? Here is the code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using Microsoft.Win32;
using System.Timers;
using System.Threading;
using System.IO;
using System.Security;
namespace SchedulerService
{
public class ScheduleService : System.ServiceProcess.ServiceBase
{
private System.ComponentModel.IContainer components;
private System.Timers.Timer timer;
public ScheduleService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
timer = new System.Timers.Timer();
timer.Interval = 2000;
timer.Elapsed += new ElapsedEventHandler( ServiceTimer_Tick );
}
// The main entry point for the process
[STAThread]
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
ScheduleService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// ScheduleService
//
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.ServiceName = "SchedulerService";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
/// set the timer interval and start the service
timer.AutoReset = true;
timer.Enabled = true;
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
timer.AutoReset = false;
timer.Enabled = false;
}
/// <summary>
/// when the timer is trigered check if there are any programs to run
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ServiceTimer_Tick(object sender,
System.Timers.ElapsedEventArgs e)
{
try
{
// Console.WriteLine("Starting...");
timer.Enabled = false;
FTPFactory ff = new FTPFactory();
ff.setDebug(true);
ff.setRemoteHost(System.Configuration.ConfigurationSettings.AppSettings["Rem
oteHost"]);
ff.setRemoteUser(System.Configuration.ConfigurationSettings.AppSettings["set
RemoteUser"]);
ff.setRemotePass(System.Configuration.ConfigurationSettings.AppSettings["set
RemotePass"]);
ff.login();
ff.chdir("cameras");
string[] fileNames = ff.getFileList("*.*");
for(int i=0;i < fileNames.Length-1;i++)
{
string thecam = fileNames;
string thcamVal = "D:/wwwroot/inetpub/test/" + thecam;
//Console.WriteLine(fileNames);
ff.download(thecam,thcamVal);
}
ff.setBinaryMode(true);
ff.close();
timer.Enabled = true;
}
catch(Exception ee)
{
//Console.WriteLine("Caught Error :"+ee.Message);
}
EventLog.WriteEntry( "ScheduleService Example Timer Function called" );
}
}
}
minutes and then download the files. Then repeat every 2 minutes. The files
just seem to stop downloading. Does that timer go off every 2 minutes if the
files are done or not? Here is the code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using Microsoft.Win32;
using System.Timers;
using System.Threading;
using System.IO;
using System.Security;
namespace SchedulerService
{
public class ScheduleService : System.ServiceProcess.ServiceBase
{
private System.ComponentModel.IContainer components;
private System.Timers.Timer timer;
public ScheduleService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
timer = new System.Timers.Timer();
timer.Interval = 2000;
timer.Elapsed += new ElapsedEventHandler( ServiceTimer_Tick );
}
// The main entry point for the process
[STAThread]
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
ScheduleService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// ScheduleService
//
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.ServiceName = "SchedulerService";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
/// set the timer interval and start the service
timer.AutoReset = true;
timer.Enabled = true;
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
timer.AutoReset = false;
timer.Enabled = false;
}
/// <summary>
/// when the timer is trigered check if there are any programs to run
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ServiceTimer_Tick(object sender,
System.Timers.ElapsedEventArgs e)
{
try
{
// Console.WriteLine("Starting...");
timer.Enabled = false;
FTPFactory ff = new FTPFactory();
ff.setDebug(true);
ff.setRemoteHost(System.Configuration.ConfigurationSettings.AppSettings["Rem
oteHost"]);
ff.setRemoteUser(System.Configuration.ConfigurationSettings.AppSettings["set
RemoteUser"]);
ff.setRemotePass(System.Configuration.ConfigurationSettings.AppSettings["set
RemotePass"]);
ff.login();
ff.chdir("cameras");
string[] fileNames = ff.getFileList("*.*");
for(int i=0;i < fileNames.Length-1;i++)
{
string thecam = fileNames;
string thcamVal = "D:/wwwroot/inetpub/test/" + thecam;
//Console.WriteLine(fileNames);
ff.download(thecam,thcamVal);
}
ff.setBinaryMode(true);
ff.close();
timer.Enabled = true;
}
catch(Exception ee)
{
//Console.WriteLine("Caught Error :"+ee.Message);
}
EventLog.WriteEntry( "ScheduleService Example Timer Function called" );
}
}
}