By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 2,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

thread starting then stopping immediately - access to socket denied?

P: n/a
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!
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"karl" <ka**********@tricaresw.af.mil> wrote in message
news:d2**************************@posting.google.c om...
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/de...on_account.asp
Nov 15 '05 #2

P: n/a


----- 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.
Nov 15 '05 #3

P: n/a
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);
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.