Question MessageBox displays too soon

Will Gilbert

Member
Joined
Dec 1, 2020
Messages
6
Programming Experience
5-10
I have following code:
C#:
for (int i = 0; i < MyCount; i++)
{
    // Do stuff which can take from 10 to 300 seconds
}

Messagebox.Show("Finished Loop");
Problem is that the MessageBox displays right away, and does not wait for loop to finish
How do I ensure Loop Finished BEFORE displaying messagebox
 
Last edited by a moderator:
That is literally impossible. The call to MessageBox.Show is after the loop so it simply cannot be executed before the loop is complete. Perhaps you have async method calls inside the loop that continue to execute in the background after the loop completes but that doesn't mean that the loop hasn't completed. You need to take a good look at the code inside that loop and make sure it actually is what you want it to be. If you can't work it out, show us the code.
 
Also, what's the value of MyCount?
 
C#:
        static int MyCount = 0;
        public static void Main(string[] args)
        {
            for (int i = 0; i < MyCount; i++)
            {
                Debug.WriteLine("Hit");
            }
        }
Notice in the above code, that "Hit" is never reached. It will only be hit if the number of MyCount is greater than 0. If MyCount is 0 or a negative number to 0, it won't execute the debug line.
 
If i equals zero, and i is less than your variable, then i will increment, and your iteration cycle will be entered.
But if i equals zero, and i is meant to be equal to zero or less than your variable value, consider adding <= (less than equal to) too your variable number, and it will enter your iteration cycle. For example; if MyCount is 5, then the iteration cycle would run 6 times. The reason for this is because you are starting i from zero. Hopefully this helps you understand your statement better.
 
That is literally impossible. The call to MessageBox.Show is after the loop so it simply cannot be executed before the loop is complete. Perhaps you have async method calls inside the loop that continue to execute in the background after the loop completes but that doesn't mean that the loop hasn't completed. You need to take a good look at the code inside that loop and make sure it actually is what you want it to be. If you can't work it out, show us the code.
The loop runs fine, I can see it because I display the loop counter on the screen as it is progressing. The counter is a row number, say its 100,after about 5, the Message Box displays, but the loop keeps going to the end.
I think it might be because the loop is running in one thread, and the MessageBox in another.(maybe in UI thread).
Would this be possible, and if so, could I get all the code to run in one thread.
 
I think it might be because the loop is running in one thread, and the MessageBox in another.(maybe in UI thread).
Would this be possible, and if so, could I get all the code to run in one thread.
The loop is not "running in another thread". You can't magically run part of a method on one thread and another part on another thread. Look at the code. It's all right there in front of you, in sequence. The loop gets executed first and then the message gets shown after. That's it, that's all. As I already said, it's possible that you call one or more other methods inside that loop that are executed asynchronously but, if so, all the loop does is start those threads. The loop is finished once all the threads have been started, not once they all finish. That's the whole point of asynchronous code. I asked you to show us the code then but you haven't done that. If you want help then you ought to provide us with the information that we ask for.

As an example of what I'm talking about consider this. Let's say that you have ten boulders and I tell you that, once you move them from point A to point B, I will give you a piece of cake. In that case, you would push one boulder to point B, then another and so on until they were all moved, then you could claim your cake. In that case, all the work must be completed first, then you get the cake. Now consider the case where the boulders are at the top of a hill and you have to move them to the bottom. In that case, you can just push all ten off the top of the hill and claim your cake, even though some or all of the boulders may still be rolling down the hill and not reached the bottom yet. In that second case, you are the loop and you have finished before you get the cake, even though the operations you started are still going on. Now, show us the code I asked for over a day ago.
 

Latest posts

Back
Top Bottom