本文介绍了Thread.Abort的似乎不扔,因为AcceptSocket的一个ThreadAbortException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的线程中调用 ChannelServer.ListeningThread.Abort ,不过好像没有什么改变。我想更具体,但我想不出什么了。似乎有没有 ThreadAbortException 时引发,而这种异常应考虑阻塞听众抛出(它完美的作品上是blockingly接收线程)。

重要修改:以 ManualResetEvent.WaitOne 而不是 AcceptSocket ,因为Lyrik已经提示了测试,它完美的作品。为什么 AcceptSocket 块中的 ThreadAbortException

链接:本次论坛主题似乎讨论了同样的问题,虽然我不明白什么出来呢:的

  ChannelServer.ListeningThread =新主题(新的ThreadStart(委托()
{
    Log.Inform(等待的线程{0}客户。Thread.CurrentThread.ManagedThreadId);

    而(真)
    {
        尝试
        {
            新主题(新ParameterizedThreadStart(ChannelClientHandler.Initialize)).Start(ChannelServer.Listener.AcceptSocket());
        }
        赶上(ThreadAbortException)
        {
            Log.Inform(中止客户端侦听线程{0},Thread.CurrentThread.ManagedThreadId);
            打破;
        }
    }
}));
ChannelServer.ListeningThread.Start();
 

解决方案

这个作品,但它是令人难以置信的草率和线程浪费。任何人都可以只点我一个方法来抛出一个异常AcceptSocket不会自动捉?

  ChannelServer.ListeningThread =新主题(新的ThreadStart(委托()
{
    Log.Inform(等待的线程{0}客户。Thread.CurrentThread.ManagedThreadId);

    而(真)
    {
        尝试
        {
            ChannelServer.ClientConnected.Reset();
            ChannelServer.Listener.BeginAcceptSocket(新的AsyncCallback(ChannelClientHandler.EndAcceptSocket),ChannelServer.Listener);
            ChannelServer.ClientConnected.WaitOne();
        }
        赶上(ThreadInterruptedException)
        {
            Log.Inform(中断客户端侦听线程{0},Thread.CurrentThread.ManagedThreadId);
            打破;
        }
    }
}));
ChannelServer.ListeningThread.Start();
 

I am calling ChannelServer.ListeningThread.Abort on the following thread, however nothing seems to happen. I would like to be more specific, but I can't think of anything more. There seems to be no ThreadAbortException that is thrown, and this exception should be thrown regardless of the blocking listener (it works perfectly on threads that are blockingly-receiving).

Important EDIT: With a ManualResetEvent.WaitOne instead of AcceptSocket, as Lyrik has suggested for testing, it works perfectly. How come AcceptSocket blocks the ThreadAbortException?

LINK: This forum thread seems to discuss the same issue, although I cannot figure anything out of it: http://www.tek-tips.com/viewthread.cfm?qid=319436&page=413

ChannelServer.ListeningThread = new Thread(new ThreadStart(delegate()
{
    Log.Inform("Waiting for clients on thread {0}.", Thread.CurrentThread.ManagedThreadId);

    while (true)
    {
        try
        {
            new Thread(new ParameterizedThreadStart(ChannelClientHandler.Initialize)).Start(ChannelServer.Listener.AcceptSocket());
        }
        catch (ThreadAbortException)
        {
            Log.Inform("Aborted client listening thread {0}.", Thread.CurrentThread.ManagedThreadId);
            break;
        }
    }
}));
ChannelServer.ListeningThread.Start();
解决方案

This works, but it's incredibly sloppy and thread-wasting. Could anyone just point me to a way to throw an exception that "AcceptSocket" won't automatically catch?

ChannelServer.ListeningThread = new Thread(new ThreadStart(delegate()
{
    Log.Inform("Waiting for clients on thread {0}.", Thread.CurrentThread.ManagedThreadId);

    while (true)
    {
        try
        {
            ChannelServer.ClientConnected.Reset();
            ChannelServer.Listener.BeginAcceptSocket(new AsyncCallback(ChannelClientHandler.EndAcceptSocket), ChannelServer.Listener);
            ChannelServer.ClientConnected.WaitOne();
        }
        catch (ThreadInterruptedException)
        {
            Log.Inform("Interrupted client listening thread {0}.", Thread.CurrentThread.ManagedThreadId);
            break;
        }
    }
}));
ChannelServer.ListeningThread.Start();

这篇关于Thread.Abort的似乎不扔,因为AcceptSocket的一个ThreadAbortException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 17:20