How do I interrupt a process?

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
I have a process that runs in my application and I'd like to be able to interrupt it so that I can view a form which contains relevant information and is normally minimised. It's easy enough to interrupt the process, but not easy to get it to start again at the place it was interrupted. It seems that Thread.Suspend is now obsolete and doesn't get evaluated.

I found this thread http://www.xtremedotnettalk.com/showthread.php?t=96994&highlight=suspend+thread but that only seems to anticipate interrupting the process each time it gets to the end. I want to be able to interrupt it in the middle and then continue where I left off. Can this be done?

Here's some code that may explain better what I'm trying to do:

Code:
Public Class Form1
  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.WindowState = FormWindowState.Minimized
        Count()
    End Sub

    Dim g As Integer
    Dim q As Integer

    Private Sub Count()

        Do
            g = g + 1
            Me.Text = g
            If Wait(1000) Then Exit Sub

            'I'd like to be able to restart at this point, not the beginning.

            q = q + 2
            Me.Text = q
            If Wait(500) Then Exit Sub

            System.Windows.Forms.Application.DoEvents()
        Loop
    End Sub

    Private Function Wait(ByVal t As Integer) As Boolean
        System.Threading.Thread.Sleep(t)
        If Me.WindowState <> FormWindowState.Minimized Then Return True
    End Function

    Private Sub cmdContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdContinue.Click
        Me.WindowState = FormWindowState.Minimized
        Count()
    End Sub
End Class
 
Are you wanting to effectively pause the background process and then resume it or to simply get information from it while it is working?

If the former you might want to wrap the state used in the function in it's own class and use one of the thread syncronisation primatives to indicate to the background thread that it should pause (e.g. at the start of the loop in the thread you could try to aquire a Monitor or check a waithandle which could be signalled from the main thread when you want the background thread to pause).

Alternatively in you simply need information from the background thread you could have it raise an event that contains the relevant information needed by the main thread.
 
It's the former. You use a lot of concepts I'm not familiar with in the second paragraph so I'm thinking this might be more work than I want to put into it. I don't need it that much...
 
Back
Top