Re: Threading
From: QPatrik_L=F6wendahl_=5BC=23_MVP=5D=22?= (patrik.lowendahl.no.freakin.spam_at_home.se)
Date: 11/21/04
- Next message: Justin Rogers: "Re: SerializationSurrogate problem with Arrays"
- Previous message: gregbacchus: "SerializationSurrogate problem with Arrays"
- In reply to: Jon Skeet [C# MVP]: "Re: Threading"
- Next in thread: Willy Denoyette [MVP]: "Re: Threading"
- Reply: Willy Denoyette [MVP]: "Re: Threading"
- Messages sorted by: [ date ] [ thread ]
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()
>
- Next message: Justin Rogers: "Re: SerializationSurrogate problem with Arrays"
- Previous message: gregbacchus: "SerializationSurrogate problem with Arrays"
- In reply to: Jon Skeet [C# MVP]: "Re: Threading"
- Next in thread: Willy Denoyette [MVP]: "Re: Threading"
- Reply: Willy Denoyette [MVP]: "Re: Threading"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|