Windows service og Timer
Hej Experter,Jeg er ved at lave en windows service som hvert 10'ende sekund skal tjekke om der sidder noget i drev G. Men jeg har lidt problemer med at se hvor jeg skal placere timeren for at den bliver kørt?
Her er min kode:
public Service1()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
if(!System.Diagnostics.EventLog.SourceExists("DoDyLogSourse"))
{
System.Diagnostics.EventLog.CreateEventSource("DoDyLogSourse","DoDyLog");
}
eventLog1.Source = "DoDyLogSourse";
eventLog1.Log ="DoDyLog";
}
// The main entry point for the process
private static Random rng = new Random();
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 MyNewService.Service1()};
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
Timer t = new Timer(new TimerCallback(Test), null, 0, 10000);
while(true) Thread.Sleep(10);
}
static void Test(Object o)
{
Thread.Sleep(rng.Next(10) * 1000);
FileStream fs = new FileStream(@"c:\skriv.txt" , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
if (Directory.Exists("G:\\"))
{
m_streamWriter.WriteLine("G mounted "+ DateTime.Now + "\n");
}
else
{
m_streamWriter.WriteLine("G unmounted "+ DateTime.Now+ "\n");
}
m_streamWriter.Flush();
m_streamWriter.Close();
fs.Close();
/*Process myProcess = new Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.Start();*/
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
//
// eventLog1
//
this.eventLog1.Log = "DoDyLog";
this.eventLog1.Source = "DoDyLogSource";
//
// Service1
//
this.ServiceName = "USB Viruskontrol";
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
}
