[Mono-list] Threading
Ian McCullough
ipmccmono@hotmail.com
Mon, 13 May 2002 12:04:06 -0400
This behavior should not be. The following is an excerpt from the .NET
Framework Developers Guide
"Thread.Start submits an asynchronous request to the system, and the call
returns immediately, possibly before the new thread has actually started.
You can use Thread.ThreadState and Thread.IsAlive to determine the state of
the thread at any one moment. Thread.Abort aborts a thread, marking it for
garbage collection. The following code example creates two new threads to
call instance and static methods on another object."
So, regardless of how csc behaves, the right way to do things is to
explicitly wait using synchronization devices. If anyone can come up with a
good explanation for why csc waits, I'm all ears. I suspected it might have
somethign to do with being static, which it doesn't, and in finding that out
I made the following mods, which showed that the main thread state while the
other thread was executing was Stopped, Background, and WaitSleepJoin. As
was said before, this should not be. It should be noted that if you put an
explicit call in Main() to Thread.CurrentThread.Abort() after the call to
oc.Run() it does indeed kill the child thread while exiting the process. The
thing that makes this bother me so much is that I seem to remember being
very frustrated early on trying to figure out why my thread was getting
killed, before I knew I had to explicitly wait on it. Oh well...
using System;
using System.Threading;
namespace testMT
{
public class testMT
{
public static void Main()
{
otherclass oc = new otherclass();
oc.Run();
}
}
public class otherclass
{
public Thread _t;
public Thread _owner;
public void Run()
{
_owner = Thread.CurrentThread;
Thread _t = new Thread(new ThreadStart(go));
_t.Start();
Console.WriteLine("Main thread exiting");
}
private void go()
{
for (int _i = 1; _i <= 1000; _i++)
if (_i %100 == 0)
{
Console.WriteLine("thread still alive: {0}",
_i.ToString());
Console.WriteLine("Parent Thread State: " +
_owner.ThreadState.ToString());
}
else
Thread.Sleep(10);
Console.WriteLine("thread exiting");
}
}
}