Solved

Cancel or abort long running operations

Posted on 2014-01-28
6
217 Views
Last Modified: 2014-02-12
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

0
Comment
Question by:iamtgo3
  • 2
  • 2
  • 2
6 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 39815115
Hi  iamtgo3;

You can use the BackgroundWork Class to run your long running code. At the bottom of the link for  BackgroundWork Class you will find a couple of examples and one showing how to cancel.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39815297
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.
0
 
LVL 1

Author Comment

by:iamtgo3
ID: 39815403
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39815481
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.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39816819
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.
0
 
LVL 1

Author Comment

by:iamtgo3
ID: 39854073
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.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get Client IP on RDS - VB.NET 15 68
Regex validation 2 28
cannot connect to sqlserver 8 35
ASP.NET (VB) return a record 2 38
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question