FileWatcher

T

tshad

If I start FileWatcher on a thread, do I need to kill it in the OnStop
method when someone presses stop?

I have the code:

ThreadStart st = new ThreadStart(InitializeFileWatcher);
workerThread = new Thread(st);
workerThread.Name = "Startup Thread for Label Print
Service";

//start the thread

workerThread.Start();

And the InitializFileWatcher:

private void InitializeFileWatcher()
{
fileWatcher = new FileSystemWatcher();
fileWatcher.Path =
ConfigurationManager.AppSettings["FolderToWatch"];
fileWatcher.NotifyFilter = NotifyFilters.FileName;
fileWatcher.Filter = "*.*";
fileWatcher.Created += HandleLabelsFromFileWatcher;
fileWatcher.EnableRaisingEvents = true;
}

I then have:

protected override void OnStop()
{
System.Diagnostics.EventLog.WriteEntry(this.ServiceName,
this.ServiceName + " stopped.",
System.Diagnostics.EventLogEntryType.Information);
}

Do I need to have something in my OnStop to either kill the thread and/or
stop the FileWatcher?

I find that when I do it this way, sometimes the Service will stop right
away or will never stop and I get an error - but the service is still in the
task managers window.

Thanks,

Tom
 
M

Mr. Arnold

tshad said:
If I start FileWatcher on a thread, do I need to kill it in the OnStop
method when someone presses stop?

I have the code:

ThreadStart st = new ThreadStart(InitializeFileWatcher);
workerThread = new Thread(st);
workerThread.Name = "Startup Thread for Label Print
Service";

//start the thread

workerThread.Start();

And the InitializFileWatcher:

private void InitializeFileWatcher()
{
fileWatcher = new FileSystemWatcher();
fileWatcher.Path =
ConfigurationManager.AppSettings["FolderToWatch"];
fileWatcher.NotifyFilter = NotifyFilters.FileName;
fileWatcher.Filter = "*.*";
fileWatcher.Created += HandleLabelsFromFileWatcher;
fileWatcher.EnableRaisingEvents = true;
}

I then have:

protected override void OnStop()
{
System.Diagnostics.EventLog.WriteEntry(this.ServiceName,
this.ServiceName + " stopped.",
System.Diagnostics.EventLogEntryType.Information);
}

Do I need to have something in my OnStop to either kill the thread and/or
stop the FileWatcher?

I find that when I do it this way, sometimes the Service will stop right
away or will never stop and I get an error - but the service is still in
the task managers window.


You need to disable everything the thread is running, and then you need to
stop the thread, which should be happening on the OnStop(),

If something is processing on the thread, then you should set a bool
process_flag to true and then set to it false when a process is done.

On the Onstop, you should go into a while loop at the top of the OnStop() on
the process_flag if it's set to true and continue with the OnStop when the
flag turns false.

That way, you ensure that a safe shutdown has occurred.





__________ Information from ESET NOD32 Antivirus, version of virus signature database 4077 (20090514) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
M

Mr. Arnold

tshad said:
If I start FileWatcher on a thread, do I need to kill it in the OnStop
method when someone presses stop?

I have the code:

ThreadStart st = new ThreadStart(InitializeFileWatcher);
workerThread = new Thread(st);
workerThread.Name = "Startup Thread for Label Print
Service";

//start the thread

workerThread.Start();

And the InitializFileWatcher:

private void InitializeFileWatcher()
{
fileWatcher = new FileSystemWatcher();
fileWatcher.Path =
ConfigurationManager.AppSettings["FolderToWatch"];
fileWatcher.NotifyFilter = NotifyFilters.FileName;
fileWatcher.Filter = "*.*";
fileWatcher.Created += HandleLabelsFromFileWatcher;
fileWatcher.EnableRaisingEvents = true;
}

I then have:

protected override void OnStop()
{
System.Diagnostics.EventLog.WriteEntry(this.ServiceName,
this.ServiceName + " stopped.",
System.Diagnostics.EventLogEntryType.Information);
}

Do I need to have something in my OnStop to either kill the thread and/or
stop the FileWatcher?

I find that when I do it this way, sometimes the Service will stop right
away or will never stop and I get an error - but the service is still in
the task managers window.


You need to disable everything the thread is running, and then you need to
stop the thread, which should be happening on the OnStop(),

If something is processing on the thread, then you should set a bool
process_flag to true and then set to it false when a process is done.

On the Onstop, you should go into a while loop at the top of the OnStop() on
the process_flag if it's set to true and continue with the OnStop when the
flag turns false.

That way, you ensure that a safe shutdown has occurred.





__________ Information from ESET NOD32 Antivirus, version of virus signature database 4077 (20090514) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 

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