Solved

Closing a form  in a clean way

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

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange - Deleted Resource Info appears in Azure Portal 6 92
recursion example 16 141
CONDITIONAL MAX STATEMENT IN T-SQL 3 73
Need a complete list of ASP.NET page load events 3 59
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

830 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