Solved

Closing a form  in a clean way

Posted on 2014-09-09
4
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

623 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