2

I'm looking for a way to make my program wait for a page to load completely, before it keeps going and executes more code on the loaded document.

I didn't find any good way to do it, but I remembered that when we use MessageBox.Show("some string"); the program stops/waits(?) and then keeps going, even in a loop.

So when we have this loop:

for (int i = 0; i < 10; i++)
{
    MessageBox.Show("i");
}

the i++ will not run until I press the OK button.

How were they able to do that?

Servy
  • 202,030
  • 26
  • 332
  • 449
samy
  • 1,949
  • 6
  • 39
  • 64
  • do you have experience with multi treading? – Emmanuel N Nov 05 '12 at 19:06
  • a little, but isnt that stop the main Thread? – samy Nov 05 '12 at 19:11
  • If you want to wait/stop you'll need to know how long. Do you want to wait a fixed amount of time. Do you want to wait until some particular event happens (if so, what), if you want to wait until another task/thread/process is done, you'll need to elaborate on *what* you are waiting on, as it can vary widely depending on the context. Also note that chances are you don't actually want to *wait* for anything, you probably just want to run some code when an asynchronous operation finishes rather than actually blocking the current thread. – Servy Nov 05 '12 at 19:11
  • 2
    The picked the wrong name for the method, they should have called it "ShowDialog". – Hans Passant Nov 05 '12 at 19:13
  • @HansPassant I guess the modality is implied in the `MessageBox`. But I agree that it is inconsistent with the methods used for showing a `Form`. – Robert Harvey Nov 05 '12 at 19:57

3 Answers3

4

MessageBox.Show() displays a modal window. The call to MessageBox.Show() blocks until the dialog is dismissed by the user.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
2

EDIT: Yes, dialogs aren't just a while loop in .NET. I was merely trying convey a simple explanation of why MessageBox.Show blocks like the question creator asked. I modified the below wording to better fit.

If you need to wait for a page to load completely you need add in some sort of trigger to determine when the page is finished.

for (int i = 0; i < 10; i++)
{
     MessageBox.Show("i");
}

Could be written as:

for(int i = 0; i < 10; i++)
{
    bool isRunning = true;
    while(isRunning)
    {
       isRunning = CheckIfSomethingIsStillRunning();
       Thread.Sleep(10);
    }
}

Basically, you have to keep a loop going until you deem it necessary that you want your Thread/Program to continue.

The reason why MessageBox.Show() blocks is because Windows are really just running in a while loop constantly receiving messages and painting. When you show a Dialog (Window.ShowDialog(), MessageBox.Show()), there is now a method that continues to block waiting for the window to be closed.

I hope this helps.

TyCobb
  • 8,909
  • 1
  • 33
  • 53
  • that intresting and now im a little confuse becuse, i read the link that servy add to his comment, and the guy that answer said: "ShowDialog(). It is DoEvents() that allows a dialog to be modal without it freezing the rest of the windows in the app." but if i use Thread.Sleep(10) it will freez all the app, right? – samy Nov 05 '12 at 19:38
  • Sorry, yes it isn't just a while loop. I was more going off of a simple way to explain it. Thread.Sleep will only pause the thread that Thread.Sleep was called on. So if it is in your main thread, then yes your whole app will pause, but if you are using another thread to handle the process then only that one will be paused. – TyCobb Nov 05 '12 at 20:01
0

generally speaking, the code behind Microsoft library functions are not exposed, but here's some psudocode how one might accomplish a similar thing

Show()
{
    DisplayMessageBox();

    while(Ok button is not pressed)
    {
        WaitAFewMilliseconds();
    }
    HideMessageBox();
}

there are other paradigms too like event-based input.

Now the reason why this doesn't hang up everything else is because of multi-threading. suppose you have 2 threads that run at the same time. the Operating system will run a few lines of one thread, and then a few lines of the other thread, so that they appear to be happening at the same time.

Now, your dialog box is probably in the same thread as that which called it (that's why i++ isn't executing), but whatever is detecting your input can be in a different thread.

  • 2
    `Now the reason why this doesn't hang up everything else is because of multi-threading` Now that's just not true. A modal popup doesn't use another thread, or another synchronization context at all. It simply uses DoEvents to avoid blocking the UI. [Link](http://stackoverflow.com/questions/5181777/use-of-application-doevents). Two key points are the "disable parent forms" at the start and "enable parent forms" at the bottom. – Servy Nov 05 '12 at 19:16
  • Oh, and while this is somewhat interesting, it's not at all how he'll want to solve his program. He'll almost certainly want to rely on adding a continuation to the asynchronous task, or waiting on a handle provided by that task, or something else along those lines. – Servy Nov 05 '12 at 19:19