tcpLisetener +线程问题 [英] tcpLisetener + Thread problem

查看:42
本文介绍了tcpLisetener +线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


让我说我的母亲在线程中有这样的东西:


线程thread1 =新线程(新的ThreadStart(this.run));

thread1.Start();

//一些不重要的代码

thread1.Abort();

thread1.Join();


在thread1中我有:


试试{

tcpListener = new TcpListener(this.port);

tcpListener.Start();

tcpClient = tcpListener.AcceptTcpClient();

//更不重要的代码

} catch(ThreadAbortException){

tcpClient.Stop();

//不确定我是否需要上面一行,但那不是问题

无论如何

tcpListener.Stop();

}


现在发生的事情是thread1启动,转到

" tcpListener.AcceptTcpClient()"呼叫并等待客户端连接。



中,所有线程的母亲到达thread1.Abort()的平均时间。并且

引发了ThreadAbortException ......并且没有任何反应。 tcpListener

仍然

挂起并等待连接,并阻止ThreadAbortException

发生!现在,这不应该是不可能的吗?我被教导要坚信

相信全能的例外......这里的事情非常错误......



有人开导我,拜托! :-)


BTW,是的,我知道,我可以创建一个连接到服务器的套接字

并取消阻止监听器,但是''肮脏和丑陋的黑客。应该是一个优雅的解决方案,用于中断tcpListener ...不应该吗?


-

" Stara boljka se leci starim lekom ...

Dabome vinom,ta nebi valjda mlekom?"

Hello there,

Lets say I have something like this in my Mother Of All Threads:

Thread thread1 = new Thread(new ThreadStart(this.run));
thread1.Start();
//some unimportant code
thread1.Abort();
thread1.Join();

And in thread1 I have:

try {
tcpListener = new TcpListener(this.port);
tcpListener.Start();
tcpClient = tcpListener.AcceptTcpClient();
//more unimportant code
} catch (ThreadAbortException) {
tcpClient.Stop();
//not sure whether I need the line above, but that''s not the problem
anyway
tcpListener.Stop();
}

What happens now is thread1 gets started, gets to the
"tcpListener.AcceptTcpClient()" call and waits for a client to connect.
In
the mean time Mother Of All Threads gets to the "thread1.Abort()" and
raises the ThreadAbortException... and nothing happens. tcpListener
still
hangs and waits for the connection, and blocks the ThreadAbortException
from occuring! Now, shouldn''t that be impossible? I was taught to firmly
believe in the Almighty Exception... something''s very wrong here...
would
somebody enlighten me, please! :-)

BTW, yes, I know, I could make a socket which will connect to the server
and unblock the Listener, but that''s a dirty and ugly hack. There should
be some elegant solution for interupting tcpListener... shouldn''t it?

--
"Stara boljka se leci starim lekom...
Dabome vinom, ta nebi valjda mlekom?"

推荐答案

tcp监听器调用接受套接字对象,然后使用P / Invoke调用

a本机方法。


我会想象异常不要前往本地方法和

因此线程不会被中止。我经历过类似的行为

与SqlDataAdapter.Fill但从未找到解决方案..


-

Patrik L ?wendahl [C#MVP]
www.cshrp.net - "优雅代码由诙谐的程序员

Nikola Skoric" < NI ******* @ net4u.hr>在消息中写道

新闻:MPG.1bd38843429e2d1989950@localhost ...
The tcp listener calls accept on a socket object which in turn calls out to
a native method using P/Invoke.

I would imagine that the exception don''t travel to the native method and
therefore the thread wont be aborted. I''ve experienced similar behaviour
with SqlDataAdapter.Fill but never found the solution..

--
Patrik L?wendahl [C# MVP]
www.cshrp.net - "Elegant code by witty programmers"
"Nikola Skoric" <ni*******@net4u.hr> wrote in message
news:MPG.1bd38843429e2d1989950@localhost...
你好,

让我说我妈妈有这样的事情所有线程:

线程thread1 =新线程(新ThreadStart(this.run));
thread1.Start();
//一些不重要的代码
thread1.Abort();
thread1.Join();

在thread1中我有:

尝试{
tcpListener = new TcpListener(this .port);
tcpListener.Start();
tcpClient = tcpListener.AcceptTcpClient();
//更多不重要的代码
} catch(ThreadAbortException){
tcpClient .Stop();
//不确定我是否需要上面这一行,但这不是问题

tcpListener.Stop();
}

现在发生的是thread1启动,进入
" tcpListener.AcceptTcpClient()"呼叫并等待客户端连接。
同时,所有线程的母亲都会到达thread1.Abort()。并且
引发了ThreadAbortException ......并且没有任何反应。 tcpListener
仍然挂起并等待连接,并阻止ThreadAbortException发生!现在,这不应该是不可能的吗?我被教导要坚定地相信全能的例外......这里的某些事情是非常错误的......
会有人启发我,拜托! :-)

BTW,是的,我知道,我可以创建一个连接到服务器的套接字
并取消阻止监听器,但这是一个肮脏和丑陋的黑客。应该有一些优雅的解决方案来中断tcpListener ......不应该吗?

-
Stara boljka se leci starim lekom ...
Dabome vinom,ta nebi valjda mlekom?"
Hello there,

Lets say I have something like this in my Mother Of All Threads:

Thread thread1 = new Thread(new ThreadStart(this.run));
thread1.Start();
//some unimportant code
thread1.Abort();
thread1.Join();

And in thread1 I have:

try {
tcpListener = new TcpListener(this.port);
tcpListener.Start();
tcpClient = tcpListener.AcceptTcpClient();
//more unimportant code
} catch (ThreadAbortException) {
tcpClient.Stop();
//not sure whether I need the line above, but that''s not the problem
anyway
tcpListener.Stop();
}

What happens now is thread1 gets started, gets to the
"tcpListener.AcceptTcpClient()" call and waits for a client to connect.
In
the mean time Mother Of All Threads gets to the "thread1.Abort()" and
raises the ThreadAbortException... and nothing happens. tcpListener
still
hangs and waits for the connection, and blocks the ThreadAbortException
from occuring! Now, shouldn''t that be impossible? I was taught to firmly
believe in the Almighty Exception... something''s very wrong here...
would
somebody enlighten me, please! :-)

BTW, yes, I know, I could make a socket which will connect to the server
and unblock the Listener, but that''s a dirty and ugly hack. There should
be some elegant solution for interupting tcpListener... shouldn''t it?

--
"Stara boljka se leci starim lekom...
Dabome vinom, ta nebi valjda mlekom?"



我已经挖了一下。


TcpListener类上有一个停止方法。 stop方法关闭,

抛弃底层套接字。尝试在侦听器上调用Stop而不是
中止线程。


另一种杀死线程的方法是使用API​​ TerminateThread但是

是一种潜在的灾难,因为你不知道本地

代码打开了哪些资源并且没有机会关闭或释放。


第三个解决方案可能是一个完全矫枉过正,你可以让线程

在一个单独的appdomain中运行并卸载appdomin本身,这可能会终止,这可能是
终止线程,但你不能保证。


-

Patrik L?wendahl [C#MVP]
www.cshrp.net - 诙谐的程序员优雅的代码


" Nikola Skoric < NI ******* @ net4u.hr>在消息中写道

新闻:MPG.1bd38843429e2d1989950@localhost ...
I''ve dug around for a bit.

There''s a stop method on the TcpListener class. The stop method closes and
throws away the underlying socket. Try calling Stop on the listener instead
of abort the thread.

Another way is to kill the thread is to use the API TerminateThread but that
is a potential catastrophy since you wouldn''t know what resources the native
code has opened and won''t have the chance to close or release.

The third solution is probably a total overkill, you could let the thread
run in a separate appdomain and unload the appdomin itself, this might
terminate the thread but you have no guarantee.

--
Patrik L?wendahl [C# MVP]
www.cshrp.net - "Elegant code by witty programmers"

"Nikola Skoric" <ni*******@net4u.hr> wrote in message
news:MPG.1bd38843429e2d1989950@localhost...
你好,

让我说我妈妈有这样的事情所有线程:

线程thread1 =新线程(新ThreadStart(this.run));
thread1.Start();
//一些不重要的代码
thread1.Abort();
thread1.Join();

在thread1中我有:

尝试{
tcpListener = new TcpListener(this .port);
tcpListener.Start();
tcpClient = tcpListener.AcceptTcpClient();
//更多不重要的代码
} catch(ThreadAbortException){
tcpClient .Stop();
//不确定我是否需要上面这一行,但这不是问题

tcpListener.Stop();
}

现在发生的是thread1启动,进入
" tcpListener.AcceptTcpClient()"呼叫并等待客户端连接。
同时,所有线程的母亲进入thread1.Abort()。并且
引发了ThreadAbortException ......并且没有任何反应。 tcpListener
仍然挂起并等待连接,并阻止ThreadAbortException发生!现在,这不应该是不可能的吗?我被教导要坚定地相信全能的例外......这里的某些事情是非常错误的......
会有人启发我,拜托! :-)

BTW,是的,我知道,我可以创建一个连接到服务器的套接字
并取消阻止监听器,但这是一个肮脏和丑陋的黑客。应该有一些优雅的解决方案来中断tcpListener ......不应该吗?

-
Stara boljka se leci starim lekom ...
Dabome vinom,ta nebi valjda mlekom?"
Hello there,

Lets say I have something like this in my Mother Of All Threads:

Thread thread1 = new Thread(new ThreadStart(this.run));
thread1.Start();
//some unimportant code
thread1.Abort();
thread1.Join();

And in thread1 I have:

try {
tcpListener = new TcpListener(this.port);
tcpListener.Start();
tcpClient = tcpListener.AcceptTcpClient();
//more unimportant code
} catch (ThreadAbortException) {
tcpClient.Stop();
//not sure whether I need the line above, but that''s not the problem
anyway
tcpListener.Stop();
}

What happens now is thread1 gets started, gets to the
"tcpListener.AcceptTcpClient()" call and waits for a client to connect.
In
the mean time Mother Of All Threads gets to the "thread1.Abort()" and
raises the ThreadAbortException... and nothing happens. tcpListener
still
hangs and waits for the connection, and blocks the ThreadAbortException
from occuring! Now, shouldn''t that be impossible? I was taught to firmly
believe in the Almighty Exception... something''s very wrong here...
would
somebody enlighten me, please! :-)

BTW, yes, I know, I could make a socket which will connect to the server
and unblock the Listener, but that''s a dirty and ugly hack. There should
be some elegant solution for interupting tcpListener... shouldn''t it?

--
"Stara boljka se leci starim lekom...
Dabome vinom, ta nebi valjda mlekom?"



" Nikola Skoric" < NI ******* @ net4u.hr>在消息中写道

新闻:MPG.1bd38843429e2d1989950@localhost ...
"Nikola Skoric" <ni*******@net4u.hr> wrote in message
news:MPG.1bd38843429e2d1989950@localhost...
你好,

让我说我妈妈有这样的事情所有线程:

线程thread1 =新线程(新ThreadStart(this.run));
thread1.Start();
//一些不重要的代码
thread1.Abort();
thread1.Join();

在thread1中我有:

尝试{
tcpListener = new TcpListener(this .port);
tcpListener.Start();
tcpClient = tcpListener.AcceptTcpClient();
//更多不重要的代码
} catch(ThreadAbortException){
tcpClient .Stop();
//不确定我是否需要上面这一行,但这不是问题

tcpListener.Stop();
}

现在发生的是thread1启动,进入
" tcpListener.AcceptTcpClient()"呼叫并等待客户端连接。
同时,所有线程的母亲进入thread1.Abort()。并且
引发了ThreadAbortException ......并且没有任何反应。 tcpListener
仍然挂起并等待连接,并阻止ThreadAbortException发生!现在,这不应该是不可能的吗?我被教导要坚定地相信全能的例外......这里的某些事情是非常错误的......
会有人启发我,拜托! :-)

BTW,是的,我知道,我可以创建一个连接到服务器的套接字
并取消阻止监听器,但这是一个肮脏和丑陋的黑客。应该有一些优雅的解决方案来中断tcpListener ......不应该吗?
Hello there,

Lets say I have something like this in my Mother Of All Threads:

Thread thread1 = new Thread(new ThreadStart(this.run));
thread1.Start();
//some unimportant code
thread1.Abort();
thread1.Join();

And in thread1 I have:

try {
tcpListener = new TcpListener(this.port);
tcpListener.Start();
tcpClient = tcpListener.AcceptTcpClient();
//more unimportant code
} catch (ThreadAbortException) {
tcpClient.Stop();
//not sure whether I need the line above, but that''s not the problem
anyway
tcpListener.Stop();
}

What happens now is thread1 gets started, gets to the
"tcpListener.AcceptTcpClient()" call and waits for a client to connect.
In
the mean time Mother Of All Threads gets to the "thread1.Abort()" and
raises the ThreadAbortException... and nothing happens. tcpListener
still
hangs and waits for the connection, and blocks the ThreadAbortException
from occuring! Now, shouldn''t that be impossible? I was taught to firmly
believe in the Almighty Exception... something''s very wrong here...
would
somebody enlighten me, please! :-)

BTW, yes, I know, I could make a socket which will connect to the server
and unblock the Listener, but that''s a dirty and ugly hack. There should
be some elegant solution for interupting tcpListener... shouldn''t it?




我不知道为什么ThreadAbortException没有''工作(虽然也许是

与讨论的问题有关$ / b $ b新闻:Ou ************** @ TK2MSFTNGP12 .phx.gbl),但我用来中止的方法

TcpListener是从主线程中调用tcpListener.Stop()(即来自你的母亲的
)线程)。这会在

监听线程(你的thread1)中引发一个SocketException。我不确定这是否100%安全,

因为从一个不同的线程调用TcpListener的方法而不是

它可能导致一些令人讨厌的竞争条件 - 但是AFAIK它是

在实践中从来没有出错。


Chris Jobson



I don''t know why the ThreadAbortException doesn''t work (though maybe it''s
related to the problem discussed in
news:Ou**************@TK2MSFTNGP12.phx.gbl), but the method I use to abort
the TcpListener is to call tcpListener.Stop() from the main thread (i.e.
from your Mother Of All Threads). This raises a SocketException in the
listening thread (your thread1). I''m not sure whether this is 100% safe,
since calling a method of the TcpListener from a different thread than the
one it was created on may lead to some nasty race condition - but AFAIK it''s
never gone wrong in practice.

Chris Jobson

这篇关于tcpLisetener +线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆