0

My WinForms App Needs a Loading Panel. I am using the example found here

(flowLayoutPanel1 is on the UI Thread The async delegate code that replaces Thread.Sleep in the example is similar to code below. I get cross thread errors adding my dynamic control to the flow panel)

AsyncProcessDelegate d = delegate() {
     flowLayoutPanel1.Controls.Clear();

Panel p1 = new Panel();
Button btn1 = new Button();

        p1.Controls.Add(btn1);

        //tried to access from another thread
        flowLayoutPanel1.Controls.Add(p1);
        // Just in case it's not clear, I already know this does not work
        // The question is how can I make the example wait for my UI to load
        // if I can't do the dynamic control creation as shown.
  };

  RunAsyncOperation(d);
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    You can't access the controls from another thread, and that's why you're getting the exception. What is the question here? – Lasse V. Karlsen Mar 01 '16 at 23:24
  • The question is how can I make the example wait for my UI to load if I cant do the dynamic control creation as shown. I have some ideas but I have been grinding on this and approaching a deadline. – Trey Paschal Mar 01 '16 at 23:28
  • What do you want your UI loading? the above controls don't need to "wait". – NoName Mar 02 '16 at 00:00
  • _"how can I make the example wait for my UI to load"_ - sort of defeats the purpose of async? –  Mar 02 '16 at 00:11
  • @TreyPaschal - UI is very very fast to create. If yours is slow it is because you are loading data from somewhere. Perhaps you need to load your data asynchronously and finish rendering your UI after it has loaded. – Enigmativity Mar 02 '16 at 07:03
  • Thanks just now seeing this. Here is what I did Enigmativity thanks for the great response. I moved all of the panels and buttons to a class of there own in my UIHelper. The async task creates them, then the UI loads them when the it is done. – Trey Paschal Mar 02 '16 at 11:49

1 Answers1

-2

this is not a good practice but this will solve ur problem definably

this.Control.CheckForIllegalCrossThreadCalls=false;
Tanmay Nehete
  • 2,138
  • 4
  • 31
  • 42