Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 601069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:40:29+00:00 2026-05-13T16:40:29+00:00

I was trying to postpone adding controls to my main form, with a goal

  • 0

I was trying to postpone adding controls to my main form, with a goal to speed up it’s start time. Well I run in the following exception:

Cross-thread operation not valid:
Control ‘Form1’ accessed from a thread
other than the thread it was created
on.

I tried to simply the problem on a smaller example but the problem stays. Here is my code:

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace AddConrolFromAnotherThread {
    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
        }


        private void AddButton() { 
            if(this.InvokeRequired){
                this.Invoke(new MethodInvoker(this.AddButton));
            }
            Random random = new Random(2);
            Thread.Sleep(20);
            Button button = new Button();
            button.Size = new Size(50,50);
            button.Location = 
                new Point(random.Next(this.Width),random.Next(this.Height));
                this.Controls.Add(button);
        }

        private void buttonStart_Click(object sender, EventArgs e) {
            Thread addControlThread = 
                new Thread(new ThreadStart(this.AddButton));
            addControlThread.Start();
        }
    }
}

I did use the Invoke method and did check if InvokeRequiered is true, but InvokeRequiered keep staying “true”. I really don’t understand that. At least I would expect StackOverflow exception, since this is a recursion call.

So, if anyone met the similar problem, please could you tell me what did I do wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:40:29+00:00Added an answer on May 13, 2026 at 4:40 pm

    The problem in your code is that your are adding two buttons.

    Put the code after the if block in an else block.

    private void AddButton() { 
            if(this.InvokeRequired){
                this.Invoke(new MethodInvoker(this.AddButton));
            }
            else {
               Random random = new Random(2);
               Thread.Sleep(20);
               Button button = new Button();
               button.Size = new Size(50,50);
               button.Location = new Point(random.Next(this.Width),random.Next(this.Height));
               this.Controls.Add(button);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to change input type attribute from password to text . $('.form').find('input:password').attr({type:text}); Why this
trying to figure out how to add zeroes in front of a random generated
Trying to create an app that does some socket communication (Writing only). I can
Trying to assign a key to each field in android to reference in Java.
Trying to get this to function, randomly chooses 1 of the 3 color choices.
Trying to find some information on the GC. In my log i see these
Trying to track down a bug in an application, but need to confirm whether
Trying to figure out out how to overload parenthesis on a class. I have
Trying to build a legacy code in VS2005 and get errors in VC header
Trying to replace any non alpha-numeric characters with a hyphen. Can't see why it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.