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

PLS HELP - Unable to close a "stuck" thread

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


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

Opa
"MuZZy" wrote:
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

Nov 16 '05 #2

P: n/a
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 Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"MuZZy" <le*******@yahoo.com> wrote in message
news:CN********************@rcn.net...
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

Nov 16 '05 #3

P: n/a
Ignacio Machin ( .NET/ C# MVP ) wrote:
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
Nov 16 '05 #4

P: n/a
Opa wrote:
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

Opa
"MuZZy" wrote:

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

Nov 16 '05 #5

P: n/a
If you don't want to use async reads, then just shut the application.
The thread and the listener will be terminated by Windows.

"MuZZy" <le*******@yahoo.com> wrote in message news:7u********************@rcn.net...

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

Nov 16 '05 #6

P: n/a
jeff wrote:
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

"MuZZy" <le*******@yahoo.com> wrote in message news:7u********************@rcn.net...
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


Nov 16 '05 #7

P: n/a
Simply close the listener socket, the AcceptSocket will throw an exception
you simply can catch and as such terminate the thread.

Willy.

"MuZZy" <le*******@yahoo.com> wrote in message
news:1Y********************@rcn.net...
jeff wrote:
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

"MuZZy" <le*******@yahoo.com> wrote in message
news:7u********************@rcn.net...
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



Nov 16 '05 #8

P: n/a
Willy Denoyette [MVP] wrote:
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
Willy.

"MuZZy" <le*******@yahoo.com> wrote in message
news:1Y********************@rcn.net...
jeff wrote:
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

"MuZZy" <le*******@yahoo.com> wrote in message
news:7u********************@rcn.net...
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

Nov 16 '05 #9

P: n/a

"MuZZy" <le*******@yahoo.com> wrote in message
news:M_********************@rcn.net...
Willy Denoyette [MVP] wrote:
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


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.


Nov 16 '05 #10

P: n/a
Willy Denoyette [MVP] wrote:
"MuZZy" <le*******@yahoo.com> wrote in message
news:M_********************@rcn.net...
Willy Denoyette [MVP] wrote:
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

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
Nov 16 '05 #11

P: n/a

"MuZZy" <le*******@yahoo.com> wrote in message
news:Xf********************@rcn.net...
Willy Denoyette [MVP] wrote:
"MuZZy" <le*******@yahoo.com> wrote in message
news:M_********************@rcn.net...
Willy Denoyette [MVP] wrote:

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

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


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.

Nov 16 '05 #12

P: n/a
Willy Denoyette [MVP] wrote:
"MuZZy" <le*******@yahoo.com> wrote in message
news:Xf********************@rcn.net...
Willy Denoyette [MVP] wrote:
"MuZZy" <le*******@yahoo.com> wrote in message
news:M_********************@rcn.net...
Willy Denoyette [MVP] wrote:
>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

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

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!
Nov 16 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.