Wednesday, March 29, 2006

ThreadPool.QueueUserWorkItem

If you look at the documentation for ThreadPool.QueueUserWorkItem you'll notice that it returns a bool. A question rose internally as to when does this actually return false? Curiousity peaked, I started an investigation. I couldn't find any reason that this would return false and if you look around in the v1 Rotor it seems that it throws on error. So I wrote a sample program to test out the issue.


List list = new List();
int len = 100000;
while (len != 100)
{
try
{
Console.WriteLine("Moving to {0}", len);
while (true)
{
list.Add(new long[len]);
}
}
catch (OutOfMemoryException)
{

}

len /= 10;
}

try
{
while (true)
{
if (!ThreadPool.QueueUserWorkItem(
new WaitCallback(CallBack)))
{
Console.WriteLine("Returned false");
}
else
{
Console.WriteLine("Successfully Queued");
}
}

}
catch (OutOfMemoryException)
{
Console.WriteLine("Threw an exception");
}
finally{
SomeMethod(list);
}

After running this program I get the following output.

Moving to 100000
Moving to 10000
Moving to 1000
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Successfully Queued
Threw an exception

So it would seem that QueueUserWorkItem doesn't actually ever returned false. This was later confirmed by a member of the CLR team and the documentation for this method will be updated in a future release.

Wednesday, March 22, 2006

Which namespace?

A question popped up today: "What's the best way to find out the namespace of various types?". Of course the framework documentation is handy here but another great way is to just use monad :)

[AppDomain]::CurrentDomain.GetAsesmblies() foreach { $_.GetTypes(); } where { $_.Name -like "SecurityIdentifier" }