/// <summary> /// Unloads the private AppDomain and the wkhtmltopdf library, and if applicable, destroys /// the synchronization thread. /// </summary> /// <param name="sender">Typically a null value, not used in the method.</param> /// <param name="e">Typically EventArgs.Empty, not used in the method.</param> private static void TearDownAppDomain(object sender, EventArgs e) { if (Factory.operatingDomain != null) { Func <object> del = () => { Factory.operatingDomain.DoCallBack(() => PechkinBindings.wkhtmltopdf_deinit()); return(null); }; Factory.invocationDelegate.DynamicInvoke(del); AppDomain.Unload(Factory.operatingDomain); foreach (ProcessModule module in Process.GetCurrentProcess().Modules) { if (module.ModuleName == PechkinBindings.LibFilename) { while (PechkinBindings.FreeLibrary(module.BaseAddress)) { } } } Factory.operatingDomain = null; } if (Factory.synchronizer != null) { Factory.synchronizer.Terminate(); Factory.synchronizer = null; } }