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

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

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.

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.

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.

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.

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 ####
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.