views:

72

answers:

2

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();
A: 

I'm not sure why you're getting that error, but here is a simple example that works:

ManualResetEvent mrse = new ManualResetEvent(false);
Thread test = new Thread(() =>
    {
        while (true)
        {
            try
            {
                mrse.WaitOne();
            }
            catch (ThreadAbortException)
            {
                Console.WriteLine("No problem here...");
            }
        }
    });

test.IsBackground = true;
test.Start();

Thread.Sleep(1000);
test.Abort();
Console.ReadKey();

So it works for me... I assumed you've stepped through the debugger and your break point inside the catch statement wasn't hit, is that correct?

Note: it's bad practice to call Abort, instead you should call Interrupt and handle the ThreadInterruptedException... it's much safer.

Lirik
"So it works for me... I assumed you've stepped through the debugger and your break point inside the catch statement wasn't hit, is that correct?"Correct.
Lazlo
@Lazo, where do you make the abort call?
Lirik
@Lirik: From another thread, which uses this method: ''ChannelServer.ListeningThread.Abort();''Does the fact that it comes from another thread hinder the process? If so, I guess I'll use delegates.
Lazlo
@Lazlo, in my example it comes from another thread (the main thread), so that's definitely not the problem. Is the code you posted the **simplest** example with which you're seeing the problem? There is no other code that might be causing issues?
Lirik
I am absolutely sure this is the most direct call - I can have breakpoints and log information before and after the abort call, I am absolutely SURE the abortion gets called. I have used extremely similar code for receival procedures and it works. I'm really puzzled...
Lazlo
@Lirik: Note: I just tested with Interrupt, still doesn't work. Thanks for the tip, though. :)
Lazlo
I suspect Accept is catching thread-related exceptions and performing its own bookkeeping.
Yuliy
@Yuliy: Seems to be so, although Receive on a Socket doesn't, so I smell unusual inconsistency in the framework. Maybe I'll have to do it with the built-in async. methods (BeginAccept/EndAccept).
Lazlo
as per the MSDN page, it seems simpler to just change your while loop to put the existing body inside a if (Pending) (potentially with an else that does a sleep(0) or sleep(1) or sleep(500) or something). Then you can change the 'true' to 'canceled == false' (canceled being some instance or static bool you add) then the other thread can just canceled = true instead of having to Abort() :)
James Manning
A: 

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();
Lazlo