Obtaining a Thread from ThreadID

hi
is it possible to get a thread from its thread id in .net
How can i abort a thread from another thread

Any help will be highly apprecited.
Thanks in advance.



Answer this question

Obtaining a Thread from ThreadID

  • optyler

    i have a bit different scenario .
    i m using calling fucntions asynchronously using begin invoke and endinvoke ...some time i need to cancel that without waiting for their completion and close the application normally...
    i was asking the quesion in that context .
    i hope you got my point now

    Any help will be highly apprecisted

    thanks
    tabish.


  • Paul200607

    Here is some self-explaining code:


    int threadId = 0xFF;

    Process currentProcess = Process.GetCurrentProcess();

    foreach( Thread thread in currentProcess.Threads )
    {
    if( thread.ManagedThreadId.Equals( threadId ) )
    {
    // We found you thread! And abort it.
    thread.Abort();
    }
    }



    Note: when you are using .NET Framework 1.x you there isn't a ManagedThreadId property but just a Id property.


  • Tarek Ahmed Ismail

    Then i can write a long post, but here is a blog post that covers it all writen by Justin Rogers: Asynchronous Regular Expressions using the ThreadPool and a cancellation model.


  • Obtaining a Thread from ThreadID