C# Abort()ing threads on exit for a Form
- by Gio Borje
So far I have this code run when the X button is clicked, but I'm not sure if this is the correct way to terminate threads on a form on exit.
Type t = this.GetType();
foreach (PropertyInfo pi in t.GetProperties())
{
if (pi.GetType() == typeof(Thread))
{
MethodInfo mi = pi.GetType().GetMethod("Abort");
mi.Invoke(null, new object[] {});
}
}
I keep getting this error: "An attempt has been made to free an RCW that is in use. The RCW is in use on the active thread or another thread. Attempting to free an in-use RCW can cause corruption or data loss."