1

I want to build loading form which I set logo to form and progress bar (For progress bar i build user control).

Here is my Code:

private void frmLoading_Load(object sender, EventArgs e)
    {
        this.TransparencyKey = Color.Turquoise;
        this.BackColor = Color.Turquoise;
        pLogo.Image = Image.FromFile("Logo.png");
        Task.Factory.StartNew(() => Progress());
    }

private void Progress() {
        int sleepTime = 200;
        ucProgressBar.Minimum = 0;
        ucProgressBar.Maximum = 100;
        int value = 0;
        for (value = 0; value < 10; value++)
        {
            ucProgressBar.Value = value;
            Thread.Sleep(sleepTime);
        }
        ucProgressBar.Status = "Setting";

        for (value = 10; value < 20; value++)
        {
            ucProgressBar.Value = value;
            Thread.Sleep(sleepTime);
        }
        ucProgressBar.Status = "Printer";
    }

when it execute in the first loop, it alert error:

"Cross-thread operation not valid: Control 'ucProgressBar' accessed from a thread other than the thread it was created on."

thanks for help!

MickyD
  • 13,463
  • 6
  • 39
  • 60
chan sopheap
  • 67
  • 1
  • 5
  • 1
    Possible duplicate of [Updating a Progress Bar from Another Thread](https://stackoverflow.com/questions/924108/updating-a-progress-bar-from-another-thread) – BugFinder Mar 08 '18 at 09:19
  • Possible duplicate of [How do I update the GUI from another thread?](https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread) – pritaeas Mar 08 '18 at 09:20
  • Maybe, but i don't think it's solved my question. – chan sopheap Mar 08 '18 at 09:28
  • 1
    Possible duplicate of [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – MickyD Mar 08 '18 at 09:31
  • i have to use that solution but still not work(i mean it is not error but the interface not display until everything is done! – chan sopheap Mar 08 '18 at 09:51

1 Answers1

0

The one of solution for executing multiple functions is using Task.WhenAll but your code should be properly rewritten for this.

kasuocore
  • 101
  • 1