Solved

Closing a form  in a clean way

Posted on 2014-09-09
4
129 Views
Last Modified: 2014-09-09
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
0
Comment
Question by:Sam OZ
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40311671
what you need to do is in the form closing request, you have to set a boolean on global scope of the form, and in the loop, check that boolean and break out of the loop (using break statement in C#, in vb.net it's exit loop I think)

a second boolean to flag if that process is ongoing or not, and in the form's closing event, cancel the closing only if the large process is ongoing

this should do the job
0
 

Author Comment

by:Sam OZ
ID: 40312094
Yes !! And thanks
0
 
LVL 40
ID: 40312572
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.
0
 

Author Comment

by:Sam OZ
ID: 40313546
Thanks James. Indeed a really useful tip
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question