Send feedback on this topic.
Teradata.Client.Provider
Cancel Method
Example 



Teradata.Client.Provider Namespace > TdCommand Class : Cancel Method
Cancels (Aborts) the execution of a TdCommand object.
Syntax
'Declaration
 
Public Overrides NotOverridable Sub Cancel() 
'Usage
 
Dim instance As TdCommand
 
instance.Cancel()
public override void Cancel()
public:
void Cancel(); override 
Exceptions
ExceptionDescription
TdConnection is not in Open state.
TdCommand is closed / disposed.
Remarks
Aborts or cancels the execution of a long running query or the result set associated with the active TdDataReader. The TdDataReader.Read method returns false after the Cancel returns. Cancel returns if TdCommand is not in execution state or there is no active data reader.
Example
The following example creates a TdConnection class and illustrates the use of the Cancel method. One thread executes the command and displays the result while the other thread cancels the command.
class ConsoleApp
{
    static void Main(string[] args)
    {
        // Create a new instance of cancelTest class.
        // Note that the query will return 10,000 rows.
        cancelTest test = 
            new cancelTest("Data Source=Teradata1;User ID=ab;Password=ab;", 
            "Select StatementText From AccLogTbl sample 10000");
            
        // Start the test.
        test.Start();
    }
            
}
            
class cancelTest
{
    private TdConnection cn;
    private TdCommand cmd;
            
    public cancelTest(String connectionString, String commandText)
    {
        // Initialize the connection object
        cn = new TdConnection(connectionString);
            
        // Initialize the command object.
        cmd = new TdCommand(commandText, cn);
    }
            
    public void Start()
    {
        Thread executeThread = new Thread(new ThreadStart(this.ExecuteQuery));
        Thread cancelThread = new Thread(new ThreadStart(this.CancelQuery));
            	
        executeThread.Start();
        cancelThread.Start();
            
        executeThread.Join();
        cancelThread.Join();
    }
            
    public void ExecuteQuery()
    {
        try
        {
            // open a session to Teradata
            cn.Open();
            
            // Execute the query to retrieve 10,000 rows.
            TdDataReader reader = cmd.ExecuteReader();
            
            // write 10,000 rows to console
            int recordNumber = 1;
            while(reader.Read())
            {
                Console.WriteLine("row[{0}], column[1] = {1}", 
                    recordNumber++, reader.GetString(0));
            }
            
            // Close the reader
            reader.Close();
        }
        catch(TdException e)
        {
            Console.WriteLine(e.Message);
        }
        catch(SystemException e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            // close the session to Teradata
            cn.Close();
        }
    }
            
    public void CancelQuery()
    {
        try
        {
            // wait a while and let the other thread open 
            // the session and execute the query
            Thread.Sleep(30000);
            
            // Cancel the command
            cmd.Cancel();
        }
        catch(System.InvalidOperationException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
Requirements

Target Platforms: Windows 8.1, Windows 10, Windows Server 2012 R2, Windows Server 2016, Windows Server 2019

See Also

Reference

TdCommand Class
TdCommand Members