Link to home
Start Free TrialLog in
Avatar of iamtgo3
iamtgo3

asked on

Cancel or abort long running operations

I know this has probably been asked before but can not find it. I am making some sample for later use and this is one I have done in the past but can not find it. I have a form with a start and stop button. When you click start it reads from a in file and the loops through and populates a textbox. The reason for the stop button is to stop the operation if needed. Some files can have well over 100,000 lines and we need a way to stop or abort operation in start button click event.

Public Class frmScratchPad

    Structure TicketInfoResults
        Dim stTicketNumber As String
        Dim stTicketDate As String
        Dim stTicketTime As String
    End Structure

    Dim mSLIR As New TicketInfoResults 

    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click

        Try

            Dim stLine As String = String.Empty
            Dim iCount As Integer = 0

            FileOpen(1, Path.GetDirectoryName(Application.ExecutablePath) & "\files\export.dat", OpenMode.Input, OpenAccess.Read)

            txtResults.Clear()
            txtCount.Clear()

            While Not EOF(1)

                Input(1, mSLIR.stTicketNumber)
                Input(1, mSLIR.stScaleDate)
                Input(1, mSLIR.stScaleTime)
                
                txtResults.Text = mSLIR.stTicket & ", " & _
                                  mSLIR.stTicketDate & ", " & _
                                  mSLIR.stTicketTime & ", " & _
                                  txtResults.Text

                iCount += 1
                txtCount.Text = "Count : " & CStr(iCount)

                txtResults.Refresh()
                txtCount.Refresh()

                Application.DoEvents()
            End While

            MessageBox.Show("All Done Now")

            FileClose(1)

        Catch ex As Exception
            'This saves the error to a file so I can look at it later.
            Me.Cursor = System.Windows.Forms.Cursors.Default
            'This saves the error to a file so I can look at it later.

            Dim myError As String

            myError = "FormName: " & ActiveForm.Name.ToString & _
                       vbCrLf & vbCrLf & "Error Message: " & ex.Message & _
                       vbCrLf & vbCrLf & "Error Type: " & ex.GetType.ToString & _
                       vbCrLf & vbCrLf & "Error Source: " & ex.Source.ToString & _
                       vbCrLf & vbCrLf & "Error Stacktrace:" & ex.StackTrace.ToString

            MessageBox.Show(ex.Message, "Start Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        Finally

        End Try

    End Sub

    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        
         'Stop, cancel or abort code goes here.

    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you go the backgroundworker route, note that you would need to move the code, that updates the UI (textbox etc), to the progress report event of backgroundworker.
Avatar of iamtgo3
iamtgo3

ASKER

Fernando,

The issue I have with threads and Background workers is this "Cross-thread operation not valid: Control 'txtResults' accessed from a thread other than the thread it was created on."

I created the Fibonacci Calculator and that works. The only way I have been able to get this to work is by creating the controls via code. If access existing controls on the form I get the above error.
Hence my comment above. Only UI thread can access controls on the form so you need to use delegates with threads to update/access UI from other threads.
Hi Hi  iamtgo3;

In one of the sections in the web reference I gave states the following, "You must be careful not to manipulate any user-interface objects in your DoWork event handler. Instead, communicate to the user interface through the ProgressChanged and RunWorkerCompleted events.",  there are other ways to do this but this is the simplest of the others.
Avatar of iamtgo3

ASKER

Fernando

I am going to accept your earlier answer however it does not solve my dilemma. Yes what you offered is a good way to do what I am asking. However "Cross-thread operation not valid" and the form I am trying to do this on has 50+ controls some needed for my operation. I will have to figure out a way to create the controls via code and still have the form actually work correctly still.