1

Using c# winforms, I'm trying to make a sort of overlay. Here's what I'm testing with:

Main window has this code:

OverlayThread = new Thread(DisplayOrderOverlay);
OverlayThread.Start();

private void DisplayOrderOverlay(object obj)
    {
        ActiveOrderOverlay AOA = new ActiveOrderOverlay();
        AOA.StartLoop();
        AOA.ShowDialog();
    }

And the overlay is just a list box on a form with this code:

public void StartLoop()
    {
        while (true)
        {
            Thread.Sleep(500);
            Random r = new Random();
            listBox1.Items.Add(r.Next().ToString());
            this.Refresh();
        }
    }

I never even see the overlay, but if I pause, the loop is running.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Patrick Schomburg
  • 2,494
  • 1
  • 18
  • 46

2 Answers2

1

while (true) loop will run forever and hence freeze you app. Normally you'd break of such loop on some condition to terminate the loop.

Possibly you are looking for Timer setup for every 500ms with handler that adds random number to a list.

Side note: check out Random number generator only generating one random number for proper creation of Random.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1
AOA.StartLoop();
AOA.ShowDialog();

You're starting your loop and trying to show the dialog on the same thread, so until the loop finishes (it never will), AOA.ShowDialog() isn't called. Make the loop exit and you should see your dialog open. Or, you can also test by putting a breakpoint on the second line to see if it ever gets hit (you should have done this already).

Yatrix
  • 13,361
  • 16
  • 48
  • 78
  • Ah that's true, cant believe I didn't notice. But what is a safe time to call that loop? I can't call it on Overlay.OnLoad, because then it never finishes loading. – Patrick Schomburg Apr 27 '15 at 01:07
  • 1
    I solved my own problem right after I saw your answer. `ActiveOrderOverlay AOA = new ActiveOrderOverlay(); AOA.Show(); AOA.StartLoop();` – Patrick Schomburg Apr 27 '15 at 01:08