467,892 Members | 1,920 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,892 developers. It's quick & easy.

Thread active when service is stopping

Hi everyboby,
i wrote a c# service that every XXX minute launch a working thread on
timer events, something like this:

private void timer_Elapsed ( object status )
{
// Worker thread
System.Threading.ThreadPool.QueueUserWorkItem
( new
System.Threading.WaitCallback( _timer_Elapsed ) );
}

I have a problem when the service is stopping: if the thread is running
(work on SQL db) I MUST wait the end of the thread before stop the
service. Probably i'll have to work on OnStop event, but how? Now my
onStop method only log the service stop.
this is my onstop method:

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
string msg;
string context = "OnStop";

try
{
DumpWorkingParameters( "Service stopped",
context,
IMCleanLogDBLibrary.LogVerbosity.InfoLow,
false );
}
catch ( System.Exception ex )
{
try
{
msg = String.Format( "Service stopping error\r\n{0}",
ex.ToString() );
eventLog.WriteEntry( msg,
System.Diagnostics.EventLogEntryType.Error );
m_TheCleanLogDBmanager.InsertLogRecord(
IMCleanLogDBLibrary.LogVerbosity.Error,
context,
msg );
}
catch {}

}
}

thanks.
Matteo

Dec 21 '05 #1
  • viewed: 1968
Share:
2 Replies
If you create the Thread yourself instead of using the ThreadPool, then
you can set it's "IsBackground" attribute to true. When IsBackground is
true, the created thread dies when the thread that created it dies. To
do this use the following code.

using System.Threading;

Thread someThreadName = new Thread(new
ThreadStart(yourThreadCallBackFunction));
someThreadName.IsBackground = true;
someThreadName.Start();

and in yourThreadCallBackFunction, whatever you name it, you have the
code for your thread to run. Then you can use someThreadName.Abort() to
end your thread, or just let it kill itself when your application
closes.

Let me know if that helps and good luck!

Dec 21 '05 #2
Ok, just time to try and i'll reply gladly.... Thanks a lot and marry
christmas.
Matteo

Dec 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Matthew Groch | last post: by
25 posts views Thread by JC | last post: by
1 post views Thread by raghudr | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.