multithreaded windows service? threadstart doesn't get called

  • Thread starter Thread starter n_o_s_p_a__m
  • Start date Start date
N

n_o_s_p_a__m

I run my multithreaded classes (class library) in a standard exe host,
works with no problems; but once hosted in SCM, the threads are not
spawned, yet no exceptions are thrown. Is this some kind of
limitation?
 
n_o_s_p_a__m said:
I run my multithreaded classes (class library) in a standard exe host,
works with no problems; but once hosted in SCM, the threads are not
spawned, yet no exceptions are thrown. Is this some kind of
limitation?

No, this works fine.

You are probably getting some sort of exception in your thread proc. There
should be a try block at the outermost scope of your thread proc to catch
and somehow log exceptions thrown from inside your thread proc.

A service is just a regular exe, nothing too special. You can go into the
Main method and look at the command line. Add a command-line switch to skip
the ServiceProcess stuff, invoke your service start routine and sleep the
main thread. I always change the project type to Console Application, and
add something like this to Main:

static void Main(String[] args)
{
//check console mode
if (args.Length == 1 && (string)args[0] =="/C")
{
System.Diagnostics.Trace.Listeners.Add(new
TextWriterTraceListener(Console.Out));
MyService s = new MyService();
Trace.WriteLine("Starting Service.");
s.OnStart(args);
Trace.WriteLine("Service Started.");
do
{
System.Threading.Thread.Sleep(10000);
} while (true);

}
//normal service start stuff goes here.

That way I can always just run the service in a console window and see all
of the Trace output.

David
 
Please disregard my post (i am nospam), we forgot to call the method that
creates the threaded classes.

DOH!

David Browne said:
n_o_s_p_a__m said:
I run my multithreaded classes (class library) in a standard exe host,
works with no problems; but once hosted in SCM, the threads are not
spawned, yet no exceptions are thrown. Is this some kind of
limitation?

No, this works fine.

You are probably getting some sort of exception in your thread proc. There
should be a try block at the outermost scope of your thread proc to catch
and somehow log exceptions thrown from inside your thread proc.

A service is just a regular exe, nothing too special. You can go into the
Main method and look at the command line. Add a command-line switch to skip
the ServiceProcess stuff, invoke your service start routine and sleep the
main thread. I always change the project type to Console Application, and
add something like this to Main:

static void Main(String[] args)
{
//check console mode
if (args.Length == 1 && (string)args[0] =="/C")
{
System.Diagnostics.Trace.Listeners.Add(new
TextWriterTraceListener(Console.Out));
MyService s = new MyService();
Trace.WriteLine("Starting Service.");
s.OnStart(args);
Trace.WriteLine("Service Started.");
do
{
System.Threading.Thread.Sleep(10000);
} while (true);

}
//normal service start stuff goes here.

That way I can always just run the service in a console window and see all
of the Trace output.

David
 
Back
Top