473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 2222
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.