Click to See Complete Forum and Search --> : Interupting Do Until


PaulJayKnight
September 8th, 2009, 12:05 PM
I have the following code

Do Until [condition]
code
Loop

This loop takes sometimes up to a minute to execute. I would like to display a message box giving the user the option of terminating the loop. When I use ShowDialog the loop doesn't execute until DialogResult is returned. When I use Show, the forms buttons are not accessible until do loop is finished. How can I make this work?

Shuja Ali
September 8th, 2009, 02:43 PM
What does the Loop do. If it is a processor intensive operation, I would recommend putting this on a separate thread. I am not sure which version of .NET you are using, but if it .NET 2.0 and above, there is a component called backgroundWorker component, that will help in achieving what you want. Check out MSDN, they have nice sample too that has the similar kind of code.

HanneSThEGreaT
September 9th, 2009, 01:01 AM
I have the following code

Do Until [condition]
code
Loop

This loop takes sometimes up to a minute to execute. I would like to display a message box giving the user the option of terminating the loop. When I use ShowDialog the loop doesn't execute until DialogResult is returned. When I use Show, the forms buttons are not accessible until do loop is finished. How can I make this work?

I think you're using the wrong logic here :) What does this loop do, can you provide us with a small snippet ¿ :wave:

jlund3
September 9th, 2009, 01:15 AM
I have the following code

Do Until [condition]
code
Loop

This loop takes sometimes up to a minute to execute. I would like to display a message box giving the user the option of terminating the loop. When I use ShowDialog the loop doesn't execute until DialogResult is returned. When I use Show, the forms buttons are not accessible until do loop is finished. How can I make this work?

I am not a VB.NET guy so I can't say anything about how to implement this, but in Delphi I have done something similar. In the do until condition I include a check to see if a cancel variable has been set to true. Then every time in the loop I call something called ProcessMessages which allows events like button clicks to be handled. One of those buttons sets the cancel variable to true. So if the user presses the cancel button the loop stop incomplete. As to exact implementation I can't really help. :(

Shuja Ali
September 9th, 2009, 01:03 PM
Wouldn't the ProcessMessages section of your code make the whole operation run slowly? The idea of multi-threading is what needs to be utilized in this case.

jlund3
September 10th, 2009, 01:16 AM
Wouldn't the ProcessMessages section of your code make the whole operation run slowly? The idea of multi-threading is what needs to be utilized in this case.

Considering that the wait is on the order of minutes, no (human) user will notice the millisecond check to see if there are pending events. If we were really so concerned with speed here, the bottleneck wouldn't be Application.ProcessMessages, but the rest of the code that is already a bottle neck. So while you could use multi-threading, to answer the original question I would like to display a message box giving the user the option of terminating the loop. take a look at Application.DoEvents. From what I understand, it is equivalent to Application.ProcessMessages in Delphi.

Shuja Ali
September 10th, 2009, 01:30 PM
Considering that the wait is on the order of minutes, no (human) user will notice the millisecond check to see if there are pending events. If we were really so concerned with speed here, the bottleneck wouldn't be Application.ProcessMessages, but the rest of the code that is already a bottle neck. So while you could use multi-threading, to answer the original question take a look at Application.DoEvents. From what I understand, it is equivalent to Application.ProcessMessages in Delphi.Well actually the DoEvents eats up the CPU. So it is not only about waiting, it is also about consuming the CPU when it is not required.

A good read is written in this blog
http://blogs.msdn.com/jfoscoding/archive/2005/08/06/448560.aspx

PaulJayKnight
September 12th, 2009, 10:00 AM
Thanks