?
Solved

Cancel or abort long running operations

Posted on 2014-01-28
6
Medium Priority
?
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

771 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