PLS HELP - Unable to close a "stuck" thread

  • Thread starter Thread starter MuZZy
  • Start date Start date
M

MuZZy

Hi,

Sorry for a repeated post but i didn't receive an answer and will try to re-phrase my question:

How do i close an additional thread from the main thread,
if this additional thread is stuck waiting for a blocking operation, eg.
if in this additional thread i wait for a Tcp connection:

Socket s = tcpListener.AcceptSocket();

Thread will get stuck on this line if no incoming connection occures...
So how do i close this thread from outside in this case?
Abort doesn't work, no exception gets thrown in the thread...

Please HELP!!!

Any ideas would be appreciated!

Thank you,
Andrey
 
If this is a socket server app, why do you want to stop listening for
connections?
You could always use asynch socks.

Opa
 
Hi,

FRom your original post it seems that the problem is regarding the file
that is open and you want to close, you can do so by using the Application
events, you may hook the ThreadException event.
or if the listening thread is alive all the time you could handle the
ApplicationExit and then close the file.


Cheers,
 
Ignacio said:
Hi,

FRom your original post it seems that the problem is regarding the file
that is open and you want to close, you can do so by using the Application
events, you may hook the ThreadException event.
or if the listening thread is alive all the time you could handle the
ApplicationExit and then close the file.


Cheers,

It's not about a file, it's that i couldn't close
a thread while it's blocked waiting for a connection.

Files were mentioned to explain that i can't use thread in the background mode, because i need to close thread controllably..

Anyway, thank you for replying to my post!

Andrey
 
Opa said:
If this is a socket server app, why do you want to stop listening for
connections?
You could always use asynch socks.

I need to close listening thread when i close the app.
Your suggestion about using async socks seems to be the only correct one.
I just started looking into async use of sockets

Thank you,
Andrey
 
If you don't want to use async reads, then just shut the application.
The thread and the listener will be terminated by Windows.
 
jeff said:
If you don't want to use async reads, then just shut the application.
The thread and the listener will be terminated by Windows.

That's the problem - the listener thread isn't terminated, i still see th eprocess after i've closed the app
 
Simply close the listener socket, the AcceptSocket will throw an exception
you simply can catch and as such terminate the thread.

Willy.
 
Willy said:
Simply close the listener socket, the AcceptSocket will throw an exception
you simply can catch and as such terminate the thread.

I can not - thread execution is stuck on that line:

Socket s = tcpListener.AcceptSocket();

I can't close it just because it doesn't get past this line of code, waiting for a connection...
Actually i 've already realised i should use async connections instead of blocking execution like this.

Thank you,
Andrey
 
MuZZy said:
I can not - thread execution is stuck on that line:

Socket s = tcpListener.AcceptSocket();

I can't close it just because it doesn't get past this line of code,
waiting for a connection...
Actually i 've already realised i should use async connections instead of
blocking execution like this.

Thank you,
Andrey

Andrey,
Sorry for not being more explicit. You can close the listener socket by
calling:
tcpListener.Stop();

This will throw:
"Server Socket Exception: A blocking operation was interrupted by a call to
WSACa
ncelBlockingCall"

while blocked on an AcceptSocket().

Willy.
 
Willy said:
Andrey,
Sorry for not being more explicit. You can close the listener socket by
calling:
tcpListener.Stop();

This will throw:
"Server Socket Exception: A blocking operation was interrupted by a call to
WSACa
ncelBlockingCall"

while blocked on an AcceptSocket().

I understand that, Willy. But then you mean that i need to close the socket from the main thread?
I can't do it from within the listening thread obviousely.
So main thread needs to obtain a pointer to the listener object from the listener thread, right?

Thank you,
Andrey
 
MuZZy said:
I understand that, Willy. But then you mean that i need to close the
socket from the main thread?
I can't do it from within the listening thread obviousely.
So main thread needs to obtain a pointer to the listener object from the
listener thread, right?

Thank you,
Andrey

Yes, you have to expose your TcpListener object reference (through a
property get) on the class that creates an instance of it , and call Close
on it from any thread other than the blocked thread of course.

Willy.
 
Willy said:
Yes, you have to expose your TcpListener object reference (through a
property get) on the class that creates an instance of it , and call Close
on it from any thread other than the blocked thread of course.

Willy.

Will give it a try!
 
Back
Top