Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / September 2003

Tip: Looking for answers? Try searching our database.

unconditional thread abort

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Azmodan - 02 Sep 2003 09:48 GMT
I want to abort (kill) a thread no matter what. In the documentation it
says that if the thread is in an unmanaged code portion when the
ThreadAbortException is thrown, the system will rethrow it when managed
code is executed. But what if the thread is stuck in an unmanaged
method? how can I kill the thread?

(as a side note, Socket.Receive() appears to be unmanaged!!! )

here is a small sample that uses 2 threads that get hung in a
socket.receive and cannot be aborted. (i wanted them to get hunged!). If
the socket part is commented, it works!

using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace ConsoleApplication2
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    ///

     class MyThread
    {
         public MyThread()
         {
         }

         public void run()
         {
              int i=0;
              int k=0;
              Console.WriteLine("init thread " + this.GetHashCode());
              while(true)
              {
                   Console.WriteLine("thread " + this.GetHashCode() + "
" + i++);
                   Thread.Sleep(1000);

                   //----begin socket part---
                   Socket sock = new
Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);

                                                     //bogus address,
nothigs come from here so that the Receive method will run forever!
                   IPAddress hostIPAddress1 =
(Dns.Resolve("europe.battle.net")).AddressList[0];
sock.Connect(new IPEndPoint(hostIPAddress1,80));
                   byte[] bytes = new byte[256];
                   k=sock.Receive(bytes);
                   Console.WriteLine("Received " + k + " bytes");
                   //---end socket part---
              }
         }
    }

    class Class1
    {
         /// <summary>
         /// The main entry point for the application.
         /// </summary>
         [STAThread]
         static void Main(string[] args)
         {
              //
              // TODO: Add code to start application here
              //
              Start();
         }

         public static void Start()
         {
              MyThread myTh1 = new MyThread();
              Thread th1 = new Thread(new ThreadStart(myTh1.run));
              th1.Name="TH1";
              th1.Start();

               MyThread myTh2 = new MyThread();
              Thread th2 = new Thread(new ThreadStart(myTh2.run));
              th2.Name="TH2";
              th2.Start();

              Thread.Sleep(5000);

              th1.Abort();
              Thread.Sleep(500);
              Console.WriteLine("th1 aborted -> status: " +
th1.ThreadState.ToString());

              Thread.Sleep(6000);

              th2.Abort();
              Thread.Sleep(500);
              Console.WriteLine("th2 aborted -> status: " +
th2.ThreadState.ToString());

         }
    }
}
Dave - 02 Sep 2003 11:05 GMT
> I want to abort (kill) a thread no matter what. In the documentation it
> says that if the thread is in an unmanaged code portion when the
> ThreadAbortException is thrown, the system will rethrow it when managed
> code is executed. But what if the thread is stuck in an unmanaged
> method? how can I kill the thread?

The only absolutely guaranteed way to kill it at that point is to exit the
application.

There are other conditions where the runtime cannot abort a thread; e.g. the
thread may be suspended, and it must be running for the abort notification
to be delivered to the managed thread.

Another method you might employ is to run the thread in a separate
appdomain, and if you cannot abort it you can unload the appdomain. The
runtime goes to extra lengths to unload an appdomain that it does not do
when you just call abort, but even this will not guarantee that the thread
gets terminated.

Also, make sure you mark each thread as Background, otherwise you will have
trouble exiting your application if the thread hangs.

> (as a side note, Socket.Receive() appears to be unmanaged!!! )

You should be able to close the socket to make it return from the call to
Receive.

> here is a small sample that uses 2 threads that get hung in a
> socket.receive and cannot be aborted. (i wanted them to get hunged!). If
[quoted text clipped - 89 lines]
>      }
> }
Azmodan - 02 Sep 2003 13:29 GMT
thanx a lot
that IsBackground property really helps avoiding zombification. :)
Daniel Pravat [MSFT] - 02 Sep 2003 18:22 GMT
Pai nu prea se poate. Imagineaza un thread care incearca sa fac o alocare
din process heap, a executat EnterCriticalSection
pe lock-ul heap-ului dupa care alt thread executa TerminateThread. Nici
macar exit process un mai functioneaza dupa asta.

In CLR e posibil sa arunci un ThreadAbortException deoarece toate lock-urile
sunt mentinute de runtime. Chiar si aici nu
se mai poate garanta nimic in process si un exit e binevenit (de exemplu, sa
zicem ca un thread incearca sa faca update la
o structura si necesita multiple operatiuni ca s-o mentine coerenta - daca
threadul moare, structura nu mai are nici o valoare;
se poate scrie cod care se asteapta ca orice structura interna sa fie in
orice stare? nu cred).

Daca totusi ai nevoie, poti avea un AppDomain separat pe care sa-l descarci
cind suspectezi un hang.

Signature

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

> I want to abort (kill) a thread no matter what. In the documentation it
> says that if the thread is in an unmanaged code portion when the
[quoted text clipped - 97 lines]
>      }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.