472,783 Members | 983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 software developers and data experts.

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
2 2188
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: yabba | last post by:
w2k server... after rebooting the event viewer/application log shows multiple restarts of Active Server Pages and they continue to build in memory as each start produces a IWAM_SERVERNAME in the...
3
by: karl | last post by:
I have a windows service that creates a monitor thread which in turn creates 4 worker threads. Each thread is based upon a derived class (HL7Listener) of the TcpListener class. When running this...
3
by: Andrew Baker | last post by:
OK this has me perplexed, puzzled and bamboozled! I have a remoting service which I displayed a message box in. I then wondered what would happen if a client made a call to the service while the...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
1
by: benmorganpowell | last post by:
I have a small windows service which connects to a POP3 server at defined intervals, scans the available messages, extracts the required information and inserts the data into a SQL database. I am...
25
by: JC | last post by:
Hi People, Please I need your help. This code run a thread ok but Not close later. thanks... private void RunServer(int aPortNumber) {
2
by: tshad | last post by:
I have a Service that starts a thread that never ends. When I stop the service from the Service Applet, does it kill the thread or do I need to do it myself? If it isn't killed, what happens...
1
by: raghudr | last post by:
Hi all, I am displaying a splash screen for which i have created a thread.Since my whole project is launched by windows service and that service will start automatically at the start of the...
1
by: Chris M. Thomasson | last post by:
FWIW, here is a quick example (in the form of a fully compliable program with error checking omitted) of how I use POSIX threads within a C++ environment:...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.