We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,085 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

VS 2010 VB stop a running function

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

3
Contributors
6
Replies
1 Day
Discussion Span
3 Months Ago
Last Updated
7
Views
Question
Answered
joel.hahn
Newbie Poster
9 posts since Aug 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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

boher
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.

Reverend Jim
Carpe per diem
Moderator
3,600 posts since Aug 2010
Reputation Points: 561
Solved Threads: 447
Skill Endorsements: 32

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.

joel.hahn
Newbie Poster
9 posts since Aug 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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.

Reverend Jim
Carpe per diem
Moderator
3,600 posts since Aug 2010
Reputation Points: 561
Solved Threads: 447
Skill Endorsements: 32

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.

joel.hahn
Newbie Poster
9 posts since Aug 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 3 Months Ago by Reverend Jim and boher

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 ####
Reverend Jim
Carpe per diem
Moderator
3,600 posts since Aug 2010
Reputation Points: 561
Solved Threads: 447
Skill Endorsements: 32

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0767 seconds using 2.7MB