CancelOperation Question

jbsoundjbsound Posts: 35
edited July 26, 2006 4:17AM in SQL Toolkit Previous Versions
Being new to working with threads in VB.NET, I'm struggling with the CancelOperation functionality.

I have an application that is executing several threads, one after the previous has finished so as to not create problems. Everything works as expected.

Problem is that I have to allow the user to cancel the database synchronization process. That is where it gets hairy for me.

I've looked through all the C# templates and understand the basics of it.

What I am struggling with is calling the Session.CancelOperation (which eventually finishes the process and doesn't start any new processes in my work chain.) I need to find a way to determine when the current thread/process/session has been completely cancelled and I'm ready to provide feedback to the user (such as enabling a 'Finish' button, and the likes.)

Again, I'm working in VB.NET, so the C# side of things is a bit foggy to me.

What's a good approach to hooking into the Session.CancelOperation call and monitoring the cancellation process?




  • Hi there,
    assuming the code that you have talked about in the previous forum comments you need to add the following code when the user clicks the cancel button.
    However the code will not immeadiately stop the operation your are running. (it is upto the underlying object to decide when this will happen.)
    In this example the Session object will throw the exception OperationCancelledException, which you should catch
    private void CompareDatabases() 
           Session.CompareDatabases(db1, db2, mappings) ; 
      catch (OperationCancelledException )
          //the user pressed cancel and the operation has cancelled
    Remember that you are not on the UI thread if your are doing this in the way we discussed yesterday. So you'll have to Invoke that over to the other thread or set up some inter-thread communication.
    I think that there are other ways of getting out the exception on Thread termination.
    Hope that helps.
  • Thanks, David, that was the bit of information I needed. I should be able to tackle it now. :)

  • I got it pretty much wrapped up, with a minor issue.

    I'm using the following code to to the actual data transfer, based on your examples:
    Dim provider As New SqlProvider
                Dim block As ExecutionBlock = provider.GetMigrationSQL(Session, True)
                executor.Status = New StatusEventHandler(AddressOf UpdateProgress)
                executor.ExecuteBlock(block, My.Settings.db2Server, My.Settings.db2Name, False, UID, PWD)

    I have code implemented that catches the OperationCancelledException error on the executor thread.

    However, there seems to be a different thread that is also started, as there is no exception that gets fired on the executor thread when the user cancels the process.

    At the beginning of the data transfer there is a lot of CPU activity going on before the e.Message shows the Executing SQL message. What process is running and what would I need to cancel?


  • Hi JB,
    I think that you can set up a Status handler also on the SqlProvider.
    (so if the user presses escape, you should call CancelOperation on the SQLProvider)
    I presume that this code snipit is running in your thread and not on the main UI?
    By calling executor.ExecuteBlock this will block until the code finishes. Which should be no problem if it's in it's own thread.
    I think the issue is that you need to plumb up the GetMigrationSQL step.
    Hope that helps.
Sign In or Register to comment.