Re: Threading

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: QPatrik_L=F6wendahl_=5BC=23_MVP=5D=22?= (patrik.lowendahl.no.freakin.spam_at_home.se)
Date: 11/21/04


Date: Sun, 21 Nov 2004 23:16:07 +0100

Hmm, now I see why I was sceptic about the thread abort exception. Just
tried a similar scenario in the default domain. For that scenario, no
threadabort exception is caught by the try catch.

Could one assume then that an appdomain isn't unloaded in the same
manner as doing it manually and the process just dies taking it's
(bakground)threads with it?

test:

static void ThreadProc()
{
        try
        {
                while(true)
                {
                        Thread.Sleep(500);
                        Console.WriteLine("Tick...");
                }
                                
        }
        catch ( ThreadAbortException teax )
        {
                    StreamWriter sw = new StreamWriter("C:\\temp\\meppo.txt");

                sw.WriteLine(teax.ToString());
                                
                Thread.Sleep(10000);
                
                Console.WriteLine ( "Thread aborted " );
                Console.ReadLine();
        }
        catch ( Exception e )
        {
                Console.WriteLine ( "Exception {0}", e.Message);
        }
                
}

static void Main(string[] args)
{
        Thread t = new Thread( new ThreadStart ( ThreadProc ) );
        t.IsBackground = true;
        t.Start();
        
        Thread.Sleep ( 2000 );
}

--
Patrik Löwendahl [C# MVP]
cshrp.net - 'Elegant code by witty programmers'
cornerstone.se 'IT Training for professionals'
Jon Skeet [C# MVP] wrote:
> "Patrik Löwendahl [C# MVP]" <patrik.lowendahl.no.freakin.spam@home.se> 
> wrote:
> 
>>Are you sure about that?
>>
>>All tests I've ever done never indicates a ThreadAbortException is 
>>injected when the application domain is unloading. Isn't CLR just 
>>killing them?
> 
> 
> According to the docs for AppDomain.Unload:
> 
> <quote>
> The threads in domain are terminated using the Abort method, which 
> throws the thread an instance of ThreadAbortException. Although the 
> thread should terminate promptly, it can continue executing for an 
> unpredictable amount of time in its finally clause.
> </quote>
> 
> Here's a complete pair of programs to demonstrate it:
> 
> Test.cs - compile to test.exe
> using System;
> 
> class Test
> {
>     static void Main()
>     {
>         AppDomain dom = AppDomain.CreateDomain ("Foo");
>         
>         dom.ExecuteAssembly ("test2.exe");
>         
>         // By the time this returns, the other thread will be running
>         
>         AppDomain.Unload(dom);
>     }
> }
> 
> Test2.cs - compile to test2.exe
> using System;
> using System.Threading;
> 
> class Test2
> {
>     static void Main()
>     {
>         new Thread (new ThreadStart(ThreadJob)).Start();
>         Thread.Sleep(100);
>     }
>     
>     static void ThreadJob()
>     {
>         Console.WriteLine ("Executing in other app domain thread");
>         try
>         {
>             Thread.Sleep(2000);
>         }
>         catch (Exception e)
>         {
>             Console.WriteLine ("Got exception "+e);
>         }
>     }
> }
> 
> Results:
> Executing in other app domain thread
> Got exception System.Threading.ThreadAbortException: Thread was being 
> aborted.
>    at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
>    at Test2.ThreadJob()
> 


Relevant Pages

  • Re: Fastest way to split a file by columns?
    ... ByteBuffer buf = new ByteBuffer; ... // process exception. ... static void initializeStream() throws Exception { ... if (pos>= buf.length) { ...
    (comp.lang.java.programmer)
  • Re: Check DateTime format
    ... "Jon Skeet " wrote: ... >> format. ... >> inefficient - catching the exception that is. ... static void Time ...
    (microsoft.public.dotnet.general)
  • Re: Check DateTime format
    ... > inefficient - catching the exception that is. ... You can use a regular expression to check the format, ... static void Time ... static readonly Regex Expression = new Regex ...
    (microsoft.public.dotnet.general)
  • Re: ?RE: Could events ever ?overrun? ?or otherwise be missed by a hand?ler?
    ... You can potentially walk the delegate list and invoke by hand which can handle the exceptions and continue calling it... ... Console.WriteLine("Doh, Exception, but continuing."); ... > static void ThrowException() ...
    (microsoft.public.dotnet.framework)
  • Re: Finally not being called in a thread
    ... the try / finally clause is not honoured in this scenario, ... static void DoSomething(object state) ...
    (microsoft.public.dotnet.languages.csharp)