Solved

Closing a form  in a clean way

Posted on 2014-09-09
4
121 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now