1

I am attempting to resolve some flickering issues in the Paint event by setting DoubleBuffered = true in a form that gets opened when the user clicks a button in the main form. However, just as the new form loads I get the exception in Program.cs

"System.ArgumentException occurred in System.Drawing.dll"


"System.ArgumentException: 'Parameter is not valid.'"

in the Program.cs file

I can confirm that the Paint method does not get called when the error pops up, and the error seems to occur just as the form's constructor is completed. I'm setting DoubleBuffered = True in the Designer properties.

First form calling second form (Form1.cs)

if(toggleButton())
            {
                int numThreads = Int32.Parse(numeric_threads.Value.ToString());


                List<Section> sections = new List<Section>();
                sections.Add(new Section(0, 0, prevScreen.Bounds.Width, 100, checkbox_UseAudio.Checked, false, Int32.Parse(numeric_horizontalLEDs.Value.ToString()))); //Top
                sections.Add(new Section(0, prevScreen.Bounds.Height - 100, prevScreen.Bounds.Width, 100, false, false, Int32.Parse(numeric_horizontalLEDs.Value.ToString()))); //Bottom
                sections.Add(new Section(0, 0, 100, prevScreen.Bounds.Height, false, true, Int32.Parse(numeric_verticalLEDs.Value.ToString()))); //Left
                sections.Add(new Section(prevScreen.Bounds.Width - 100, 0, 100, prevScreen.Bounds.Height, false, true, Int32.Parse(numeric_verticalLEDs.Value.ToString())));//  Right
                dial = new LEDSimulate(pathScript, pathPython, formInst, sections, combo_monitor.SelectedIndex, numThreads);
                dial.Show();
            }
            else
            {
                button_flag = true;
                //Kill all the threads by closing
                dial.Close();
            }

Second form that gets opened form first form and results in the error (LEDSimulate.cs)

        public LEDSimulate(string pathScript, string pathPython, Form1 formInst, List<Section> sections, int mon, int numThreads)
        {
            InitializeComponent();
            instance = this;

            this.TransparencyKey = (BackColor);
            this.sections = sections;
            this.procs = new List<Process>();
            this.formInst = formInst;

            //Initialize timer to tick every 30 seconds to update UI
            timer_updatePaint.Interval = 33;

            //Load with dummy data to update later
            Pen dummy_pen = new Pen(Color.FromArgb(0, 0, 0, 0));
            foreach (Section s in sections)
            {
                for(int i = 0; i<s.subSections; i++)
                {
                    paintList.Add(new PaintForm(dummy_pen, 0, 0, 0, 0));
                }
            }
            Console.WriteLine("Finished adding and starting");
            timer_updatePaint.Start();
            //-----Once this is done, the error occurs------
        }

Program.cs

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1()); //Error pops up on this line
        }
    }
        private void timer_updatePaint_Tick(object sender, EventArgs e)
        {
            this.Refresh();
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
                return cp;
            }
        }
  • _timer_updatePaint_ Can we see the Tick ? – TaW Apr 21 '19 at 02:17
  • @TaW Added it. It only contains Refresh() to call the Paint, but this function does not get called before the error occurs – hanahouhanah Apr 21 '19 at 02:34
  • 1
    If you are using Visual Studio, go to Debug->Exceptions and mark System.ArgumentException to break when the exception is thrown. That will catch the exception when it happens and not when it is handled. – o_weisman Apr 21 '19 at 06:10
  • @o_weisman After enabling this, it still seems to break exactly on that line (Application.Run(new Form1())) with no new information (I'll update OP for something I forgot to say though) – hanahouhanah Apr 21 '19 at 10:34
  • 2
    Do you have an OnPaint handler in your code and are you disposing one of its arguments like here: https://stackoverflow.com/questions/9134181/what-could-cause-double-buffering-to-kill-my-app ? – o_weisman Apr 21 '19 at 11:20
  • The other thing you have to do is Tools > Options > Debugging > General, untick "just my code". – Hans Passant Apr 21 '19 at 15:56
  • @o_weisman Many thanks! I guess I was googling the wrong thing when trying to figure out what was wrong. I did dispose of the Graphics object (using (var gfx = e.Graphics)) in the OnPaint handler. Removing it worked. I had not realized that after the handler is called, there is some backwork being done that still requires the object. – hanahouhanah Apr 22 '19 at 04:12
  • Glad to be of assistance :) – o_weisman Apr 22 '19 at 10:56
  • @o_weisman, yes... Disposing control graphics was the problem, thanks – Eiad Samman Jan 30 '21 at 09:36

0 Answers0