Ending a sub-routine/thread from an event

I have a subroutine i am running which continually loops, pulling & formatting data. I have a button on the associated form that i want to STOP the process and end the subroutine. I used threading, which frees up other processes, but i don't know how to end a sub from another sub/event.

Thanks,

Josh



Answer this question

Ending a sub-routine/thread from an event

  • SukhiNew

    The background worker uses threading. It is the recommended way to use

    threads according to the help.

    I am an amatuer programmer, so Spotty can probably explain this much better

    than I.

    (I am a DOS programmer from way back when, and am just now getting into

    Windows programming.)



  • Biswajitghosh25

    Background worker process is a class/control which is designed to simplify the task of running a background process.

    If you comfortable using threading then I'd so go for it and continue this way.

    If you new to the idea of running a background/process and running 2005/Express then I would definately take a look at it along with the samples associated with it.


  • Jimmy Gao

    I guess the general answer is you need an exit condition on you loop to allow it to exit the loop - or you kill/terminate the thread.

    So in a single thread example. You start a infinite loop and the way to stop it is to set a condition which causes the loop to exit. In this case I'm using a Public Variable on the module.

    You'd set this property in the event

    Public Class Form1
    Private Sub StartLoopProcess(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Mod1.i = 0
    Mod1.foo()
    End Sub

    Private Sub StopLoopProcess(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Mod1.i = -1
    MsgBox(Mod1.i)
    End Sub

    Private Sub ShowLoopCounter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    MsgBox(Mod1.i)
    End Sub
    End Class


    Module Mod1
    Public i As Integer = 0

    Sub foo()
    '//Cause an infinite loop in sub
    Do While True
    Application.DoEvents()

    If i = -1 Then '//Exit Condition
    Exit Do
    Else
    i = i + 1
    End If
    Loop
    End Sub
    End Module

    I'm not sure if this is completely applicable to you example but I wouldnt think you can say - stop this sub in this thread. I dont seem to have ever seen this in any examples.


  • mortiz2112

    ^ Very interesting! Is there an advantage to using the backgroundworker vs threading (i'm pretty new to this).

    Thanks,

    Josh


  • Sudhakar.hcitek

    Thanks - i should've thought of that! Works great. :)
  • cwng

    For those who might want to use the background worker:

    Imports System

    Imports System.ComponentModel

    Imports System.Threading

    Imports System.Windows.Forms

    Public Class Form1

    Inherits Form

    Public Sub New()

    InitializeComponent()

    End Sub

    Private Sub setTextBackgroundWorkerBtn_Click( _

    ByVal sender As Object, _

    ByVal e As EventArgs) Handles setTextBackgroundWorkerBtn.Click

    Me.backgroundWorker1.WorkerSupportsCancellation = True

    Me.backgroundWorker1.RunWorkerAsync()

    End Sub

    Private Sub backgroundWorker1_RunWorkerCompleted( _

    ByVal sender As Object, _

    ByVal e As RunWorkerCompletedEventArgs) _

    Handles backgroundWorker1.RunWorkerCompleted

    Me.textBox1.Text = _

    x.ToString & " This text was set safely by BackgroundWorker."

    End Sub

    <STAThread()> _

    Shared Sub Main()

    Application.EnableVisualStyles()

    Application.Run(New Form1())

    End Sub

    Public x As Long

    Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backgroundWorker1.DoWork

    For x = 1 To 2450000000

    If backgroundWorker1.CancellationPending Then

    Exit Sub

    End If

    x += 1

    Next

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Me.backgroundWorker1.CancelAsync()

    End Sub

    End Class



  • Ending a sub-routine/thread from an event