I have a Windows Forms application that itself launches different threads to do different kinds of work. Sometimes all threads (including the user interface thread) become frozen, and my application stops responding. I decided that this could be a garbage collector problem, as the GC temporarily freezes all managed threads. To make sure that the streams just processed are frozen, I expand the unmanaged file, which is written to the "heartbeat" file with a time stamp every second, and this is not affected (i.e. Still running):
public delegate void ThreadProc();
[DllImport("UnmanagedTest.dll", EntryPoint = "MyUnmanagedFunction")]
public static extern void MyUnmanagedFunction();
[DllImport("kernel32")]
public static extern IntPtr CreateThread(
IntPtr lpThreadAttributes,
uint dwStackSize,
IntPtr lpStartAddress,
IntPtr lpParameter,
uint dwCreationFlags,
out uint dwThreadId);
uint threadId;
ThreadProc proc = new ThreadProc(MyUnmanagedFunction);
IntPtr functionPointer = Marshal.GetFunctionPointerForDelegate(proc);
IntPtr threadHandle = CreateThread(IntPtr.Zero, 0, functionPointer, IntPtr.Zero, 0, out threadId);
My question is: how can I simulate this situation when all managed threads are paused but unmanaged threads keep spinning?
:
private void button1_Click(object sender, EventArgs e) {
Thread t = new Thread(new ThreadStart(delegate {
new Hanger();
GC.Collect(2, GCCollectionMode.Forced);
}));
t.Start();
}
class Hanger{
private int[] m_Integers = new int[10000000];
public Hanger() { }
~Hanger() { Console.WriteLine("About to hang...");
}
}
!