thread starting then stopping immediately - access to socket denied?

K

karl

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 service on
my workstation I have no problems whatsoever. I then deployed this to
another server and cannot open any of the sockets. Troubleshooting
this issue I've discovered that the 4 threads do successfully enter
the HL7Listener constructor. Unfortunately the method called from
ThreadStart never gets executed. My monitor thread checks the status
of each worker thread every 10 seconds and the threadstate for each
thread is set to running temporarily.

I'm totally baffled and frustrated. This is my first attempt at
multi-threading. My logic is based upon solid examples. Again,
everything works fine on my workstation.

I've set each thread principal to the WindowsPrincipal and am writing
to the event log within the various methods to determine exactly where
my problem might lie. I'm catching every possible exception and
nothing is being thrown.

please help!
 
T

Trevor

karl said:
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 service on
my workstation I have no problems whatsoever. I then deployed this to
another server and cannot open any of the sockets. Troubleshooting
this issue I've discovered that the 4 threads do successfully enter
the HL7Listener constructor. Unfortunately the method called from
ThreadStart never gets executed. My monitor thread checks the status
of each worker thread every 10 seconds and the threadstate for each
thread is set to running temporarily.

I'm totally baffled and frustrated. This is my first attempt at
multi-threading. My logic is based upon solid examples. Again,
everything works fine on my workstation.

I've set each thread principal to the WindowsPrincipal and am writing
to the event log within the various methods to determine exactly where
my problem might lie. I'm catching every possible exception and
nothing is being thrown.

please help!

You aren't running the service under "LocalSystem" account are you? You
need to setup a domain user account to get access to anything non-trivial in
the system. Go to the Services Administrative Tool and then click on
Properties for your service. Then go to the LogOn tab and enter a domain
user account. Then try to run your service. It should work. In the
meantime check this out:
http://msdn.microsoft.com/library/d...nes_for_selecting_a_service_logon_account.asp
 
G

Guest

----- karl wrote: ----
I've set each thread principal to the WindowsPrincipal and am writin
to the event log within the various methods to determine exactly wher
my problem might lie. I'm catching every possible exception an
nothing is being thrown

are you absolutely sure that is the case. are you catching and logging exceptions in each worker thread individually? because monitor thread will not be able to catch exceptions thrown in the worker threads. otherwise, don't know what to tell you without examining the code.
 
K

karl holzman

below is the method that gets called within the ThreadStart. Also, I
forgot to mention that the HL7Listener class has a property called
ExceptionString that gets populated with whatever exception that is
handled. The monitor thread then checks to see if the ExceptionString
is set so that it can report it to the eventlog, abort the thread, reset
the ExceptionString and start a new thread. Hopefully this code formats
correctly when I submit this reply.

thanks again for the help!

public void StartListening()
{
EventLog.WriteEntry("ICDB", "entered StartListening() on port " +
this._port);
while (true)
{
EventLog.WriteEntry("ICDB", "Listening on port " + this._port);

if (!this.Active)
{
try
{
this.Start();
//Console.WriteLine("Listening for a connection...");

}
catch (SocketException se)
{
this.ExceptionString = se.ToString();
}
catch (Exception e)
{
this.ExceptionString = e.ToString();
}
}

try
{
if (!this._clientConnected)
{
client = this.AcceptSocket();
this._clientConnected = true;
}
//Console.WriteLine("Connection accepted...");
ns = new NetworkStream(client);

bool bState, bConnected = true;

while (bConnected)
{
if (ns.DataAvailable) this.GetMessage();

bState = client.Poll(1, SelectMode.SelectRead);

if (bState && client.Available == 0)
bConnected = false;
else
bConnected = true;
System.Threading.Thread.Sleep(100);
}
}
catch (ThreadAbortException)
{
this.ExceptionString = "Thread signalled to abort";
//Console.WriteLine("Thread Interrupted. Exiting thread ");
}
catch (ObjectDisposedException)
{
this.ExceptionString = "Socket closed by client.";
}
catch (SocketException se)
{
this.ExceptionString = se.ToString();
}
catch (Exception e)
{
this.ExceptionString = e.ToString();
}
finally
{
if (this.ns != null)
this.ns.Close();
if (this.client != null)
{
this.client.Close();
this._clientConnected = false;
}

if (this.DBConnected)
{
this.OraConn.Close();
this.OraConn.Dispose();
}

EventLog.WriteEntry("ICDB", "Finally Leaving StartListening() on
port " + this._port);
}
}
}
 

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