Solved

Cancel or abort long running operations

Posted on 2014-01-28
6
213 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 62

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 62

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now