I am creating a winform app that accepts a name and position as input and upon clicking the submit button generates a barcode of that name and prints that on the entered position in a pdf file(generates this as well). The app is giving me error on Environment.Exit(1) - Error Creating Window Handle in the code below -
I googled about this error and it showed that this error is related to a memory leak. I checked in the task manager. When I ran this app, the memory usage field show 7.5 kb.
This is what I am doing in my app -
There are only two textbox (name and position) and a submit button.
This is the generate PDF code
My PDF has 80 different static locations. These will never change. So, My GetCoordinates function has 80 different switch cases.
For the time being, I have replaced the exit condition with the return statement and the app works now -
But I am not able to understand where my app is creating more than 10,000 handlers ( I have read that only 10,000 handlers per app is allowed).
C#:
if(string.IsNullOrEmpty(textboxName.Text))
{
MessageBox.Show("Name cannot be empty");
Environment.Exit(1);
}
I googled about this error and it showed that this error is related to a memory leak. I checked in the task manager. When I ran this app, the memory usage field show 7.5 kb.
This is what I am doing in my app -
There are only two textbox (name and position) and a submit button.
C#:
try{
if(string.IsNullOrEmpty(textboxName.Text))
{
MessageBox.Show("Name cannot be empty");
Environment.Exit(1);
}
if(string.IsNullOrEmpty(textboxPosition.Text))
{
MessageBox.Show("Name cannot be empty");
Environment.Exit(1);
}
string name = textboxName.Text;
string position = Convert.ToInt32(textboxPosition.Text);
BarcodeWriter writer = new BarcodeWriter()
{
Format = BarcodeFormat.CODE_128,
Options = new EncodingOptions
{
Height = 100,
Width = 180,
PureBarcode = false,
Margin = 10,
},
};
string content = @"*" + name + "*";
var bitmap = writer.Write(content);
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
GeneratePDF(ms, position);
}
}
catch
{
}
This is the generate PDF code
C#:
PdfDocument document = new PdfDocument();
try{
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage pic = XImage.FromStream(ms);
string coordinates = GetCoordinates(position);
string coordinate = coordinates.Split(',');
int x = Convert.ToInt32(coordinate[0]);
int y = Convert.ToInt32(coordinate[1]);
gfx.DrawImage(pic,x , y , 110, 23);
string filename = "Output.pdf";
document.Save(@"E:\" + filename);
MessageBox.Show("PDF saved successfully!");
}
catch
{
}
My PDF has 80 different static locations. These will never change. So, My GetCoordinates function has 80 different switch cases.
C#:
GetCoordinates(position)
{
swtich(position)
{
case 1: x = 30;
y = 40;
break.
case 2: x=60;
y= 80;
break;
.
.
.
.till case 80
}
return x +"," +y
}
For the time being, I have replaced the exit condition with the return statement and the app works now -
C#:
if(string.IsNullOrEmpty(textboxName.Text))
{
MessageBox.Show("Name cannot be empty");
return;
}
But I am not able to understand where my app is creating more than 10,000 handlers ( I have read that only 10,000 handlers per app is allowed).