When I run the next code, an OutOfMemoryException is thrown:
static void Main(string[] args)
{
for (int i = 0; i < 200; i++)
{
Stream filestream = new MemoryStream(Resources.File); // Resources.File is typeof byte[]
ThreadPool.QueueUserWorkItem(ThreadProc, filestream);
}
Thread.Sleep(999999999);
}
private static void ThreadProc(object stream)
{
// Document is 3rd party's class
Document doc = new Document((Stream)stream); // OutOfMemoryException is thrown after 160 iterations
}
But, if I create the Stream inside "ThreadProc" method - there is no exception:
static void Main(string[] args)
{
for (int i = 0; i < 200; i++)
{
ThreadPool.QueueUserWorkItem(ThreadProc, null);
}
Thread.Sleep(999999999);
}
private static void ThreadProc(object stream)
{
Stream filestream = new MemoryStream(Resources.File);
Document doc = new Document(filestream); // Exception is NOT thrown
}
Why is there a difference?