Resolved application is freezing while running.

Kamen

Active member
Joined
Nov 30, 2020
Messages
28
Programming Experience
1-3
Hello.
How are you?
I'd like to ask another help now.
I made a c# windows form application, which have 2 thread and customized controls such as rounded button, gradient panel, rounded form dialog.
And also it has some additional custom dll, too.
I'll share current error text here.
I hope your professional assistance again.
Thank you.
Best Regards.
 

Attachments

  • error.txt
    8.5 KB · Views: 48
Solution
Never do this:
C#:
using (Graphics g = e.Graphics)
{
    :
}

Never dispose something that you don't own. In this case, the e.Graphics belongs to the framework code that is calling your event handler. The assignment g = e.Graphics is just taking a reference. It is not giving you a brand new [il]Graphics[/icode] object that you can dispose on your own.
OutOfMemoryException: Insufficient memory
...
path STV01.RoundedButton.OnPaint(PaintEventArgs e) path C:\Users\wolfm\Documents\stv01_dev\c--PosSystem(2021-01-06)\STV01\RoundedButton.cs:line 43
OutOfMemoryException in painting usually means not disposing graphics resources after they have been used.
 
Thanks for your kind assistance.
I think so but don't know correct reason and fixing way now.
Application is frozen in 1 day or less than 24 hour since start running in general.
Generally, when the application is first started, it rarely freezes, and it freezes after a day or a few hours.
Here is the RoundedButton cs file.
Looking forward to hear from you asap.
Thank you again.
Regards.
 

Attachments

  • RoundedButton.txt
    2.9 KB · Views: 32
What I see:
line 45: LinearGradientBrush is not disposed.
line 48: SolidBrush and StringFormat is not disposed.
line 51: SolidBrush and StringFormat is not disposed.
 
For future reference, please don't attach error messages and code but rather post them directly, formatted appropriately. Unless text is long, it's much easier for us to read it in your post rather than download an attachment. Code is syntax-highlighted when formatted properly, unlike an attached text file.

As for the issue, start employing using statements to create disposable objects and then they will be automatically disposed at the end of the block. The compiler will tell you if an object you try to create with a using statement is not disposable.
 
Thanks for your advice.
You and this c# forums always give me correct and necessary resolve within short time.
I love this forum and appreciate that you help me.
So I think it is no problem that make LinearGradientBrush dispose.
btw, currently I'm using SolidBrush and StringFormat object directly to refer in method.
In this case, how can I dispose them or employ using for these objects?
C#:
 base.OnPaint(e);
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            RectangleF Rect = new RectangleF(diffValue, diffValue, this.Width, this.Height);
            LinearGradientBrush lgb = new LinearGradientBrush(Rect, this.ColorTop, this.ColorBottom, 90F);
            Graphics g = e.Graphics;
            g.FillRectangle(lgb, Rect);
            e.Graphics.DrawString(text, Font, new SolidBrush(Color.FromArgb(255, 114, 118, 126)), Rect, new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center });

            Rect = new RectangleF(0, 0, this.Width, this.Height);
            e.Graphics.DrawString(text, Font, new SolidBrush(ForeColors), Rect, new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center });
            GraphicsPath GraphPath = null;
            using (GraphPath = GetRoundPath(Rect, radiusValue))
            {
                this.Region = new Region(GraphPath);
            }
 
Last edited:
currently I'm using SolidBrush and StringFormat object directly to refer in method.
In this case, how can I dispose them or employ using for these objects?
Add a using block for them, use these in method call.
 
Like this?
C#:
 using(SolidBrush sb = new SolidBrush(Color.FromArgb(255, 114, 118, 126)))
{     
               using(StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center })
                {
                    e.Graphics.DrawString(text, Font, sb, Rect, sf);
                }
}
 
Last edited:
while debuging with above code, I got argument exception error "Parameter is not valid".
what's wrong with me?
Could you help me with above my full code?
with example code snip employing using?
 
Last edited:
while debuging with above code, I got argument exception error "Parameter is not valid".
what's wrong with me?
You're not providing enough information. The exception tells you the problem is at line 27, that's:
C#:
GraphPath.AddArc(Rect.X, Rect.Y, radius, radius, 180, 90);
You are probably passing radius 0 here.
 
You're not providing enough information. The exception tells you the problem is at line 27, that's:
C#:
GraphPath.AddArc(Rect.X, Rect.Y, radius, radius, 180, 90);
You are probably passing radius 0 here.
So the value of radius in AddArc should not 0 as a parameter absolutely?
If I want to use 0 value too, how can I fix it?
 
If radius is 0 then just skip the GetRoundPath/Region part.
 
If radius is 0 then just skip the GetRoundPath/Region part.
Ok, I see. BTW, the argument exception error is happened in this section.
C#:
using (SolidBrush sb = new SolidBrush(Color.FromArgb(255, 114, 118, 126)))
{
    using (StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center })
    {
        e.Graphics.DrawString(text, Font, sb, Rect, sf);
    }
}

additional explanation for the error:
when I tried to employ using in LinearGradientBrush section, error pointer picked up e.Graphics.DrawString(text, Font, sb, Rect, sf);
I'll share my updated code now.
C#:
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
    RectangleF Rect = new RectangleF(diffValue, diffValue, this.Width, this.Height);
    using (LinearGradientBrush lgb = new LinearGradientBrush(Rect, this.ColorTop, this.ColorBottom, 90F))
    {
        using (Graphics g = e.Graphics)
        {
            g.FillRectangle(lgb, Rect);
        }
    }

    using (SolidBrush sb = new SolidBrush(Color.FromArgb(255, 114, 118, 126)))
    {
        using (StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center })
        {
            e.Graphics.DrawString(text, Font, sb, Rect, sf);
        }
    }

    Rect = new RectangleF(0, 0, this.Width, this.Height);
    using (SolidBrush sb = new SolidBrush(ForeColors))
    {
        using (StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center })
        {
            e.Graphics.DrawString(text, Font, sb, Rect, sf);
        }
    }
    if (radiusValue != 0)
    {
        using (GraphicsPath GraphPath = GetRoundPath(Rect, radiusValue))
        {
            this.Region = new Region(GraphPath);
        }
    }
}
 
Last edited:
I can't see that it does. My compiler was very specific about the information in post 11.
 
Back
Top Bottom