Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cancel or abort long running operations

Posted on 2014-01-28
6
Medium Priority
?
222 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 64

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 64

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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.
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

609 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