thread won't start on server

S

sir dd

Hi,

I've got a Windows-service that runs nice and smooth on my local
machine, but when installed on a server it fails to start the thread
inside. I can't figure out what's wrong.
The code below executes as it should on my machine: test123 is written
to the eventlog, but it's not on the server where it should be
installed.
It has however run for a few days on the server, but just stopped one
day (perhaps after a reboot?). The eventlog isn't showing any error
messages either - the OnStart method is executed just fine (tested that
also by writing to the eventlog). Only the mythread function doesn't
get started anymore... Does anyone have any idea how that's possible?

Thanks in advance!



using System.Diagnostics;
using System.Threading;

namespace mynamespace
{
public class myservice: System.ServiceProcess.ServiceBase
{
...
protected override void OnStart(string[] args)
{
ThreadStart ts = new ThreadStart(mythread);
Thread t = new Thread(ts);
t .Start();
}

public void mythread()
{
EventLog.WriteEntry("test123");
try
{
...
}
catch(ThreadAbortException)
{
}
}
...
}
}
 
J

Jon Skeet [C# MVP]

sir said:
I've got a Windows-service that runs nice and smooth on my local
machine, but when installed on a server it fails to start the thread
inside. I can't figure out what's wrong.
The code below executes as it should on my machine: test123 is written
to the eventlog, but it's not on the server where it should be
installed.
It has however run for a few days on the server, but just stopped one
day (perhaps after a reboot?). The eventlog isn't showing any error
messages either - the OnStart method is executed just fine (tested that
also by writing to the eventlog). Only the mythread function doesn't
get started anymore... Does anyone have any idea how that's possible?

If you put a call to EventLog.WriteEntry in your OnStart method, does
that appear in the event log?

Jon
 
J

Jon Skeet [C# MVP]

sir said:
Yes it does. Both on my development machine and on the server.

So if you put two logging lines, one before the call to Thread.Start
and one after, they both get executed - but the logging line at the
very start of the new thread *doesn't* get called, right?

Hmm. That sounds very odd indeed. I'll have a think about it...

Jon
 
W

Willy Denoyette [MVP]

| Hi,
|
| I've got a Windows-service that runs nice and smooth on my local
| machine, but when installed on a server it fails to start the thread
| inside. I can't figure out what's wrong.
| The code below executes as it should on my machine: test123 is written
| to the eventlog, but it's not on the server where it should be
| installed.
| It has however run for a few days on the server, but just stopped one
| day (perhaps after a reboot?). The eventlog isn't showing any error
| messages either - the OnStart method is executed just fine (tested that
| also by writing to the eventlog). Only the mythread function doesn't
| get started anymore... Does anyone have any idea how that's possible?
|
| Thanks in advance!
|
|
|
| using System.Diagnostics;
| using System.Threading;
|
| namespace mynamespace
| {
| public class myservice: System.ServiceProcess.ServiceBase
| {
| ...
| protected override void OnStart(string[] args)
| {
| ThreadStart ts = new ThreadStart(mythread);
| Thread t = new Thread(ts);
| t .Start();
| }
|
| public void mythread()
| {
| EventLog.WriteEntry("test123");
| try
| {
| ...
| }
| catch(ThreadAbortException)
| {
| }
| }
| ...
| }
| }
|

This:
| t .Start();
| }

wouldn't actually compile so I guess this is not your real code. Could you
post the actual OnStart and myThread methods?

Willy.
 
S

sir dd

Hi,

I've cut out some parts, because I can't post business critical code,
sorry!

protected override void OnStart(string[] args)
{
ThreadStart ts = new ThreadStart(threadlogrotation);
thrLogRotate = new Thread(ts);
thrLogRotate.Start();
}

public void threadlogrotation()
{
EventLog.WriteEntry("test123");
try
{
}
catch(ThreadAbortException)
{
}
catch(Exception err)
{
}
}
 
J

Jon Skeet [C# MVP]

sir said:
I've cut out some parts, because I can't post business critical code,
sorry!

That doesn't matter - but does the code you've posted demonstrate the
problem? If you deploy that code, does the same thing happen? We need
to see code which definitely exhibits the problem, showing everything
it can (such as that OnStart is definitely being called, and that the
code definitely reaches the end of it, and that it can write to the
event log).

Jon
 
W

Willy Denoyette [MVP]

| Hi,
|
| I've cut out some parts, because I can't post business critical code,
| sorry!
|
| protected override void OnStart(string[] args)
| {
| ThreadStart ts = new ThreadStart(threadlogrotation);
| thrLogRotate = new Thread(ts);
| thrLogRotate.Start();
| }
|
| public void threadlogrotation()
| {
| EventLog.WriteEntry("test123");
| try
| {
| }
| catch(ThreadAbortException)
| {
| }
| catch(Exception err)
| {
| }
| }
|

No problem as long as you don't cut anything in OnStart or before your
WriteEntry in your thread procedure.

What's the status of your service after you start it with the "Services"
control panel applet?

What if you do this?

thrLogRotate.Start();
Thread.Sleep(100);
EventLog.WriteEntry("Started");
}

Willy.
 
S

sir dd

I install the service via InstallUtil.exe and always start it manually
through Control Panel - Administrative Tools - Services.
That works fine and does not throw any errors.
I can also stop the service like it should be stopped.

Only the thread-method does not get executed.
WriteEntry in the thread-method does not write anything to the
EventLog, but it does in the OnStart method.

I'm now going to build a completely new project with a clean service
with only a WriteEntry call in the thread-method. But even if that
should work, it still not explains why my perfectly good service just
stopped working one day...

Thanks guys!
 

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