1

I have program that displays a "Loading" Winform when a button is pressed and disappears once the script needing to be run is complete.

When the button is pressed, the new form 'appears' however it displays none of the form information, such as the Logo and labels - only a blank/grey box. I've attempted changing the background colour and altering images however it is still displaying as blank form.

What I find to be most confusing is that this blank form displayed only appears blank when a specific CS. file is called within the button press; PDFMerge.CombineMultiblePDFs. If I try to display the Loading form within a different part of the program, e.g. when a different button is pressed, the form loads correctly as planned with all content.

Here is the blank form being displayed:

enter image description here

Here is the correct form being displayed on a different button or different form

enter image description here

Here is the code I am calling which displays the "blank" Winform.

 loadingPDF.Show(); // Show the loading form string fileDate = DateTime.Now.ToString("dd-MM-yy"); string fileTime = DateTime.Now.ToString("HH.mm.ss"); string outcomeFolder = outputFolder; string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".pdf"; string outputFileName = Path.Combine(outcomeFolder, outputFile); // combines the file name, output path selected and the yes / no for pagebreaks. PDFMerge.CombineMultiplePDFs(sourceFiles, outputFileName); loadingPDF.Hide(); // Hide the loading form 

If I replace the PDFMerge.Combine with a different within CS file, the Loading form displays correctly, which leads me to believe the issue is laying with the PDFMerge and when it is being called. Below is the code used within the PDFMerge;

public class PDFMerge { public static void CombineMultiplePDFs(String[] fileNames, string outFile) { try { int pageOffset = 0; int f = 0; Document document = null; PdfCopy writer = null; while (f < fileNames.Length) { // Create a reader for a certain document PdfReader reader = new PdfReader(fileNames[f]); reader.ConsolidateNamedDestinations(); // Retrieve the total number of pages int n = reader.NumberOfPages; pageOffset += n; if (f == 0) { // Creation of a document-object document = new Document(reader.GetPageSizeWithRotation(1)); // Create a writer that listens to the document writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create)); // Open the document document.Open(); } // Add content for (int i = 0; i < n;) { ++i; if (writer != null) { PdfImportedPage page = writer.GetImportedPage(reader, i); writer.AddPage(page); } } PRAcroForm form = reader.AcroForm; if (form != null && writer != null) { //writer.CopyAcroForm(reader); writer.Close(); } f++; } // Close the document if (document != null) { document.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } 

I don't see what could be causing the clash with the form display, perhaps the Form isn't loading on time but i don't see how it works with some features and not with others. Any advice regarding the issue would be greatly appreciated. Thank you

Update 1: Additional code requested, Here is the code used to LoadingPDF form. I used Winforms to create the content on the form:

public partial class LoadingPDF : Form { public LoadingPDF() { InitializeComponent(); } private void LoadingPDF_Load(object sender, EventArgs e) { // } } 

Creating instance of the loadingPDF form in the file selection form

 // Declaring the 'loading' form when files are being combined. LoadingPDF loadingPDF = new LoadingPDF(); 
6
  • You need to show the code where you create and instantiate the loadingPDF variable, as well as the constructor code for the loadingPDF form. Commented Apr 6, 2016 at 17:09
  • @kateract - i've added the additional information regarding the loadingPDF. Commented Apr 6, 2016 at 17:16
  • try commenting out the hide after the combine. See if the pictures are showing up after the combine is done. If it is, you are cpu locking your program before the images are loaded. Commented Apr 6, 2016 at 17:23
  • Does the form fail to show the pictures if you comment out the PDFMerge.Combine method and put in a Thread.Sleep(5000)? Commented Apr 6, 2016 at 17:23
  • i've attempted commenting out the PDFmerge.combine, however the form displayed is still not working. This includes adding the Thread.(sleep) - a blank form is instead displayed instead of the decorated form. Commented Apr 6, 2016 at 17:34

1 Answer 1

1

Building on the comments, the PDFMerge.CombineMultiplePDFs() is cpu-locking your program, causing the thread to stop loading the form before it finishes. You can adapt your code like this:

public void ShowLoading() { loadingPDF.Shown += loadingPDF_Shown; loadingPDF.Show(); // Show the loading form } public void loadingPDF_Shown(object sender, eventargs e) { string fileDate = DateTime.Now.ToString("dd-MM-yy"); string fileTime = DateTime.Now.ToString("HH.mm.ss"); string outcomeFolder = outputFolder; string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".pdf"; // combines the file name, output path selected and the yes / no for pagebreaks. PDFMerge.CombineMultiplePDFs(sourceFiles, outputFileName); loadingPDF.Hide(); // Hide the loading form } 

Shown is the last event to trigger when a form is loaded. This should load your images before you start your cpu-intensive process.



An alternative would be to put your cpu-intensive process on another thread, to keep the UI thread clear. You can do that like this:

public void ShowLoading() { loadingPDF.Show(); // Show the loading form System.ComponentModel.BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += worker_DoWork; worker.RunWorkerCompleted += worker_RunWorkerCompleted; worker.RunWorkerAsync(); //Added missed line } void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { //anything you want to do AFTER the cpu-intensive process is done loadingPDF.Hide(); // Hide the loading form } public void worker_DoWork(object sender, DoWorkEventArgs e) { string fileDate = DateTime.Now.ToString("dd-MM-yy"); string fileTime = DateTime.Now.ToString("HH.mm.ss"); string outcomeFolder = outputFolder; string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".pdf"; string outputFileName = Path.Combine(outcomeFolder, outputFile); // combines the file name, output path selected and the yes / no for pagebreaks. PDFMerge.CombineMultiplePDFs(sourceFiles, outputFileName); } 

Doing this with a background worker will keep your UI usable/clickable, and not make it freeze. Among other things, this allows for an animated loading form.

Sign up to request clarification or add additional context in comments.

9 Comments

i've tried the first method you suggested. when the button is pressed, the form displayed is still blank, when i hit the button again, the form displays correctly however the documents fail to merge and the form displayed doesn't disappear once the next message regarding total amount of documents merged is shown. i will attempt the 2nd option you suggested now.
upon trying the 2nd suggested method, the loading form is now displayed correctly, however that is all it does. The correct loading form is displayed but fails to disappear. the program now also fails to merge the documents and display the total merge undertaken.
would application.doEvents be worth pursuing when attempting to address this issue?
I added a line I previously missed in the second suggestion under ShowLoading, "worker.RunWorkerAsync();". This starts the worker to do the work, which should be what is missing.
thank you! the winform appears / works perfectly. i'm going to apply this code to other parts of my program now as the conversion process seem's quicker since breaking up the code called. thank you again
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.