• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 564
  • Last Modified:

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
0
James Atkin
Asked:
James Atkin
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now