Cannot get Windows Service to run properly with FileWatcher?

A

Aaron

I tried this with and without a timer, I got farther with a timer. THis code
works just fine in a form but only gets as far as activating StartWatcher in
the Win Service.



protected override void OnStart(string[] args)

{

// TODO: Add code here to start your service.


//timer2.Start();

StartWatcher();

EventLog.WriteEntry("OnStart","OnStart activated");

}


protected override void OnStop()

{

// TODO: Add code here to perform any tear-down necessary to stop your
service.

timer2.Stop();

EventLog.WriteEntry("OnStop","OnStop activated");

}



private void StartWatcher()

{

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"C:\input";

watcher.Filter = "*.txt";

// Add event handlers.

watcher.EnableRaisingEvents = true;

watcher.Created +=new FileSystemEventHandler(watcher_Created);

//it creates this entry in the log and thats it, when i drop the file it
doesn't pick up

EventLog.WriteEntry("StartWatcher","StartWatcher activated - next
watcher_Created");

}

private void watcher_Created(object sender, FileSystemEventArgs e)

{

//write some code here to pick up the files

ProcessFile();

EventLog.WriteEntry("watcher_Created","watcher_Created activated - File
dropped");

}

private void ProcessFile()

{


//code to process file

EventLog.WriteEntry("ProcessFile","ProcessFile activated");


}

private void timer2_elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

//not using this now

//active StartWatcher

StartWatcher();

EventLog.WriteEntry("timer2_elapsed","timer2_elapsed activated - next
startwatcher");

}



I just don't see what is wrong?
 
A

Aaron

Yeah, but i don't understand why as this exact same code works in VB?


tbain said:
Aaron,

You have 30 seconds to get a service started and return to the service
control. You probably just need to put the timer on it's own thread and call
StartWatcher from there. Your code as is never returns from the "OnStart"
method. Hope this helps.

:) Thom

Aaron said:
I tried this with and without a timer, I got farther with a timer. THis code
works just fine in a form but only gets as far as activating StartWatcher in
the Win Service.



protected override void OnStart(string[] args)

{

// TODO: Add code here to start your service.


//timer2.Start();

StartWatcher();

EventLog.WriteEntry("OnStart","OnStart activated");

}


protected override void OnStop()

{

// TODO: Add code here to perform any tear-down necessary to stop your
service.

timer2.Stop();

EventLog.WriteEntry("OnStop","OnStop activated");

}



private void StartWatcher()

{

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"C:\input";

watcher.Filter = "*.txt";

// Add event handlers.

watcher.EnableRaisingEvents = true;

watcher.Created +=new FileSystemEventHandler(watcher_Created);

//it creates this entry in the log and thats it, when i drop the file it
doesn't pick up

EventLog.WriteEntry("StartWatcher","StartWatcher activated - next
watcher_Created");

}

private void watcher_Created(object sender, FileSystemEventArgs e)

{

//write some code here to pick up the files

ProcessFile();

EventLog.WriteEntry("watcher_Created","watcher_Created activated - File
dropped");

}

private void ProcessFile()

{


//code to process file

EventLog.WriteEntry("ProcessFile","ProcessFile activated");


}

private void timer2_elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

//not using this now

//active StartWatcher

StartWatcher();

EventLog.WriteEntry("timer2_elapsed","timer2_elapsed activated - next
startwatcher");

}



I just don't see what is wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top