private void MultiDocument_Click(object sender, EventArgs e) { C1MultiDocument mdoc = new C1MultiDocument(); // make sure we can cancel the generation: mdoc.DoEvents = true; // Attach a handler to ItemGenerated to make sure all memory used by a multi-doc // item's generation is released when that generation is complete: mdoc.ItemGenerated += (ss, ee) => { GC.Collect(); GC.WaitForPendingFinalizers(); }; this._pview.Document = mdoc; ProgressForm pf = new ProgressForm(); pf.Show(); // Build the multi-document: try { FilesPrinter fp = new FilesPrinter(); fp.MakeMultiDocument(mdoc, tbDir.Text, tbMask.Text, pf); } catch (Exception ex) { MessageBox.Show(ex.Message); } // Reset cancel state of the progress form: pf.DialogResult = System.Windows.Forms.DialogResult.None; // Set up event handlers for better UI: this.FormClosing += (ss, ee) => { if (mdoc.BusyState == BusyStateEnum.Generating) { mdoc.Cancel = true; } }; mdoc.LongOperation += (ss, ee) => { pf.SetProgress(string.Format("Generating C1MultiDocument, {0:P} complete...", ee.Complete), (float)ee.Complete); if (pf.Cancelled && mdoc.BusyState == BusyStateEnum.Generating) { mdoc.Cancel = true; } }; mdoc.DocumentEnded += (ss, ee) => { pf.Hide(); this.Activate(); }; // Generate the multi-document: mdoc.Generate(); }