So I have a class that is set out like this:
public class NetworkServer
{
private BlockingCollection<byte[]> receivingQueue;
public NetworkServer(IPEndPoint endpoint, int packetsize)
{
receivingQueue = new BlockingCollection<byte[]>(new ConcurrentQueue<byte[]>());
// Do some other stuff here
}
public ~NetworkServer()
{
// Do some stuff here
Task.Factory.StartNew(() => Parallel.ForEach<byte[]>(receivingQueue, item => SomeOtherClass.ParseItem(item)));
}
}
With the Task.Factory.StartNew in the destructor, I was wondering if that would create a new thread that will continue running even though the instance of NetworkServer might be garbage collected. If this wouldn’t work what would be the best way to parse each of those items on a new thread/parse the items after the instance of NetworkServer has been GC’d. Also, ParseItem will be in a different class in-case that wasn’t clear.
Thanks.
Technically, it will.
But, that’s not a destructor. That’s finalizer.
If you’ve got a machine with 16GB of RAM, the first finalizer may be run hours after the start. That’s definitely a bad design.