Is it possible to catch a "Thread Exit" ?

Hi all,
I have an app that kicks off several threads.
As these threads complete, you see the Thread Exit text in the output window of the debugger.
Is it possible to intercept this and capture the thread ids?

Thanks,
James
LVL 4
James AtkinSenior Principle Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lazyberezovskyCommented:
Catch ThreadAbortException
0
James AtkinSenior Principle Software EngineerAuthor Commented:
Where do I bind ThreadAbortException?
Do you have a snippit of code fpr this?
Thanks....
0
lazyberezovskyCommented:
Sure

Say, you have thread function:
thread = new Thread(new ThreadStart(DoSomething));
thread.Start();

public void DoSomething()
{
    try
    {    
      // do stuff  
    }
    catch (ThreadAbortException ex)
    {
       // do something before killing thread
    }
}

when you somewere call thread.Abort, you'll fall to that catch block


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikebirtCommented:
Hi,

I assume you don't have access to change the code of the thread itself, otherwise you'd have raised some event or invoked some method directly from there to indicate the ending of the thread. So, assuming you can't change the thread but can control the calling of it, you could wrap the the thread start itself, and do a standard 'Thread Finished!' thing at the end of he thread start wrapper.

i've attached a simplistic example to demonstrate this. The UI for the form has a listbox, called listBox1.

HTH

Mike
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            listBox1.Items.Add(Thread.CurrentThread.ManagedThreadId.ToString() + " running");
            Start();
        }

        private void ThreadEnded(int threadId)
        {
            AddItem(threadId.ToString() + " ended");
            Start();
        }

        public void ThreadStartWrapper(object obj)
        {
            ThreadStart ts = obj as ThreadStart;
            ts.Invoke();

            ThreadEnded(Thread.CurrentThread.ManagedThreadId);
        }

        private void Start()
        {
            Thread t = new Thread(new ParameterizedThreadStart(ThreadStartWrapper));
            t.Start(new ThreadStart(Run));
        }

        private void Run()
        {
            Thread.Sleep(1000);
        }

        private void AddItem(string item)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(delegate() { AddItem(item); }));
            }
            else
            {
                listBox1.Items.Add(item);
            }
        }
    }

Open in new window

0
James AtkinSenior Principle Software EngineerAuthor Commented:
That works perfectly, thanks...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.