CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2008
    Posts
    52

    [RESOLVED]Interupting Do Until

    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?
    Last edited by PaulJayKnight; September 13th, 2009 at 05:18 PM. Reason: [RESOLVED]

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Interupting Do Until

    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.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Interupting Do Until

    Quote Originally Posted by PaulJayKnight View Post
    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 ¿

  4. #4
    Join Date
    Sep 2009
    Posts
    4

    Re: Interupting Do Until

    Quote Originally Posted by PaulJayKnight View Post
    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.

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Interupting Do Until

    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.

  6. #6
    Join Date
    Sep 2009
    Posts
    4

    Re: Interupting Do Until

    Quote Originally Posted by Shuja Ali View Post
    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
    Quote Originally Posted by PaulJayKnight View Post
    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.

  7. #7
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Interupting Do Until

    Quote Originally Posted by jlund3 View Post
    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/arc...06/448560.aspx

  8. #8
    Join Date
    Jul 2008
    Posts
    52

    Resolved

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured