C
C#6h ago
Silme94

Loading Form crashing (Windows Form)

So im doing a pdf viewver using winform. I wanted to add a form that indicate the progression when loading the pdf pages. The problem is that the application crash during the for loop, and the loading form is not showing up before and its showing at the end of the loop but this is useless since the content is already loaded and it close immediately. I've tried many thing like using Task.Run but i cannot fix it. (pls help)
9 Replies
Silme94
Silme946h ago
this is the loading form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PDF_Viewver
{
public partial class LoadForm : Form
{
public int Value { get => progressBar1.Value; }

public LoadForm(string reason)
{
InitializeComponent();

reasonLabel.Text = reason;
this.ControlBox = false;
}

public void UpdateValue(int value)
{
progressBar1.Value = value;

if (progressBar1.Value >= 100)
{
this.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PDF_Viewver
{
public partial class LoadForm : Form
{
public int Value { get => progressBar1.Value; }

public LoadForm(string reason)
{
InitializeComponent();

reasonLabel.Text = reason;
this.ControlBox = false;
}

public void UpdateValue(int value)
{
progressBar1.Value = value;

if (progressBar1.Value >= 100)
{
this.Close();
}
}
}
}
the function where i use that class
private void Convert(List<string> pages)
{
PagesBarPanel.Controls.Clear();
mainPDF_panel.Controls.Clear();
pdfPages.Clear();

int index = 0;

LoadForm loadscreen = new LoadForm("Loading the pdf file...");
loadscreen.Show();

Thread.Sleep(100);

foreach (string page in pages)
{
int progressValue = (int)((index + 1) * 100 / pages.Count);
loadscreen.UpdateValue(progressValue);

PictureBox box = new PictureBox
{
Size = new Size(800, 1000),
ImageLocation = page,
Anchor = AnchorStyles.None,
SizeMode = PictureBoxSizeMode.Zoom
};

mainPDF_panel.Controls.Add(box);
pdfPages.Add(box);

PictureBox page_ = new PictureBox
{
Size = new Size(180, 251),
ImageLocation = page,
Anchor = AnchorStyles.None,
SizeMode = PictureBoxSizeMode.Zoom,
Cursor = Cursors.Hand
};

Label pagesNumber = new Label
{
Text = (index + 1).ToString(),
ForeColor = System.Drawing.Color.Black,
BackColor = System.Drawing.Color.Transparent,
Margin = new Padding(0, 0, 0, 10),
TextAlign = ContentAlignment.MiddleCenter
};

int currentIndex = index;
page_.MouseDown += (sender, e) =>
{
if (currentIndex < mainPDF_panel.Controls.Count)
{
Control correspondingImage = mainPDF_panel.Controls[currentIndex];
mainPDF_panel.ScrollControlIntoView(correspondingImage);
UpdateSelectedPageInPagesBar(currentIndex);
pdf.SelectedPage = currentIndex + 1;
}
};

PagesBarPanel.Controls.Add(page_);
PagesBarPanel.Controls.Add(pagesNumber);

index++;
}

ResizeControls();
}
private void Convert(List<string> pages)
{
PagesBarPanel.Controls.Clear();
mainPDF_panel.Controls.Clear();
pdfPages.Clear();

int index = 0;

LoadForm loadscreen = new LoadForm("Loading the pdf file...");
loadscreen.Show();

Thread.Sleep(100);

foreach (string page in pages)
{
int progressValue = (int)((index + 1) * 100 / pages.Count);
loadscreen.UpdateValue(progressValue);

PictureBox box = new PictureBox
{
Size = new Size(800, 1000),
ImageLocation = page,
Anchor = AnchorStyles.None,
SizeMode = PictureBoxSizeMode.Zoom
};

mainPDF_panel.Controls.Add(box);
pdfPages.Add(box);

PictureBox page_ = new PictureBox
{
Size = new Size(180, 251),
ImageLocation = page,
Anchor = AnchorStyles.None,
SizeMode = PictureBoxSizeMode.Zoom,
Cursor = Cursors.Hand
};

Label pagesNumber = new Label
{
Text = (index + 1).ToString(),
ForeColor = System.Drawing.Color.Black,
BackColor = System.Drawing.Color.Transparent,
Margin = new Padding(0, 0, 0, 10),
TextAlign = ContentAlignment.MiddleCenter
};

int currentIndex = index;
page_.MouseDown += (sender, e) =>
{
if (currentIndex < mainPDF_panel.Controls.Count)
{
Control correspondingImage = mainPDF_panel.Controls[currentIndex];
mainPDF_panel.ScrollControlIntoView(correspondingImage);
UpdateSelectedPageInPagesBar(currentIndex);
pdf.SelectedPage = currentIndex + 1;
}
};

PagesBarPanel.Controls.Add(page_);
PagesBarPanel.Controls.Add(pagesNumber);

index++;
}

ResizeControls();
}
Silme94
Silme946h ago
No description
Cmdr. Beavis
Cmdr. Beavis6h ago
Have you tried creating a super bare bones repro of the problem?
Nasdack
Nasdack6h ago
A WinForms crashing indicates an exception Try catch the Convert method and see what exception pops up
Silme94
Silme945h ago
wdym? no i mean a little stop bc of the loop u see?
Nasdack
Nasdack5h ago
https://paste.gg/p/anonymous/4478c9c4e76f43e8b62dbffdec9fedfa You might also need to call BeginInvoke on ResizeControls() This is a CPU bound synchronous process The Convert method execution will finish before the loading completes so make sure you take care of that
Cmdr. Beavis
Cmdr. Beavis5h ago
Creating a seperate app with just enough libraries, etc. to reproduce the problem, which can make debugging a LOT easier / more simple. Also (and/or), you may want to add some logging to a local db or something in case the problem isn't something you can find via "stepping through the code." Back in the day that was the first question most people trying to help you would ask. 😁
Silme94
Silme945h ago
no but this is not rly the problem ok i will try what you sent me no... wait i can i post the project on github, install it on your machine and test it? You will have better understanding of what im trying to explain
Silme94
Silme944h ago
@Nasdack go to Form1.cs and you will found the Convert function (it will take extracted image from pdf and add it to the form) btw you need to install Apose.PDF 1.8.4 https://github.com/Silme94/pdf
GitHub
GitHub - Silme94/pdf
Contribute to Silme94/pdf development by creating an account on GitHub.
Want results from more Discord servers?
Add your server