1,105,644 Community Members

VS 2010 VB stop a running function

Member Avatar
joel.hahn
Newbie Poster
9 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

How do i stop a running function using a button or menu. Do I use a try catch? for some reason its boggling me.

Member Avatar
boher
Newbie Poster
14 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I don't exactly know what you are trying to achieve but this might help : http://goo.gl/9PKTE

Member Avatar
Reverend Jim
Noli mentula
5,459 posts since Aug 2010
Reputation Points: 761 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

If you have a block of code that is stuck in a very long loop that you want to exit prematurely then you'll need two things.

  1. a global condition that you can test to do an Exit
  2. a call to My.Application.DoEvents inside that loop

For example

Public Class Form1

    Private GetOut As Boolean
    .
    .
    .
    Private Sub MySub(...)
    .
    .
    .
        GetOut = False

        Do While some condition
            .
            .
            .
            If GetOut Then Exit Do
            .
            .
            .
            My.Application.DoEvents

        Loop

Then the code under (for example) btnEscape would include

GetOut = True

If you don't have the DoEvents call then your application will lock up.

Member Avatar
joel.hahn
Newbie Poster
9 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

nice, i will try this. basically just running a function that loops through a listview and changes the items based on a value. so to stop, i was just looking for a way to exit the function. not sure if the loop will work. because it will continuessly run until you press escape?

if it doesnt work correctly I will post my code.

Member Avatar
Reverend Jim
Noli mentula
5,459 posts since Aug 2010
Reputation Points: 761 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
1
 

If you are just looping through a listview then why do you need to exit the loop using a buttor or menu? Surely it will exit after all of the items have been processed. Mayybe you should post the code so we can make a more informed suggestion.

Member Avatar
joel.hahn
Newbie Poster
9 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

the reason for the need to stop is that I am looping through about 1k records. so if the user wanted to stop for what ever reason. then i needed a way to do it. thanks. this works perfect.

Question Answered as of 1 Year Ago by Reverend Jim and boher
Member Avatar
Reverend Jim
Noli mentula
5,459 posts since Aug 2010
Reputation Points: 761 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

If you want to speed things up then do something like

ListView1.SuspendLayout
'put the loop here to update the listview
ListView1.ResumeLayout

This will stop the display of the listview from redrawing until all of the items have been processed instead of once for each change. It could make a huge difference to the performance. In order to give visual feedback to the user you might have a label that indicates something like

Processing #### of ####
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: