Link to home
Start Free TrialLog in
Avatar of Sam OZ
Sam OZFlag for Australia

asked on

Closing a form in a clean way

Hi ,

    I  have a simple but still critical  issue

       I have a windows  application in VB.net  where  a quite time consuming operation will happen in the click of a button .
       I need to  have the following
           1) Have the form in the responding state   ( The user should be able to click an Abort button to close the application  at any time)

          2) When the Abort button is clicked , the application should exit (FROM TASK MANAGER ALSO)

          I used Application.DoEvents()  to keep the  form responsive . But then the  application, when aborted  is not exiting from Taskmanager


             My  TestCode will have just two buttons   1)WasteTime  2) Abort

             On the  Click of the WasteTime button , the following  routine runs

    '' This mimics a time consuming operation
    Sub WastingTime()
        Dim i, j, k As Integer

        For i = 0 To 10000
            For j = 0 To 10000
                For k = 1 To 10000
                   '' Application.DoEvents()     '' THIS CREATES PROBLEM AND IS COMMENTED
                Next
            Next
        Next

    End Sub


       Can you please help me to make this a responsive but still abortable form , when a time consuming operation is on

   Thanks

  Sam
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sam OZ

ASKER

Yes !! And thanks
Guy's answer is the way to go, but there might be more at stake, depending on your application.

Application.DoEvents enable the Click on your Abort button. But I would not call it in every loop, because it slows down things a lot because you are interrupting the code on each loop. Put a counter in the loop and call DoEvents only from time to time.

Then, the code in your Abort button is important. Not only should it deal with the Boolean variables proposed by Guy, but it also need to properly close the application. The best way to do it is Application.Exit. This will trigger the FormClosing event on all the currently loaded forms. Depending on how the application is structured, a Form that is still in memory but invisible can sometimes hold the application so that it still show as a process in the Task Manager.
Avatar of Sam OZ

ASKER

Thanks James. Indeed a really useful tip