RMCLink.Interop .NET Assembly

RMCLink.CancelRequest Method

This method can be used in multi-threaded applications to cancel the currently-in-progress read, write, or connect request.

Syntax

Visual Basic (Declaration)

Public Sub CancelRequest

 

Visual Basic (Usage)

Dim instance As RMCLink

 

instance.CancelRequest()

 

C#

public void CancelRequest ()

 

Visual C++/CLI

public:

void CancelRequest ()

Remarks

This method is only for use in multi-threaded applications.

This method signals the current read, write, or connection attempt (if one is in progress) to throw a CancelledException, and then returns immediately. Notice that the CancelledException may be thrown on the transaction's thread before or after this method returns on its thread. If no transaction is in progress when this method is called, then this method will do nothing and return.

This method affects, at most, one transaction (read, write, or connection attempt), so requests made after this method returns will behave normally.

Exceptions

No exceptions are generated by this method.

Example

The following code example illustrates the use of the CancelRequest method.

Visual Basic

Imports RMCLinkNET
Imports System.Threading
 
' Assumes that the form contains the following controls:
'   buttonRead - Button labeled "Read Position".
'   buttonCancel - Button labeled "Cancel" (defaults disabled).
'   textBoxActPos - Read-only TextBox that displays Axis 0 Actual Position.
'   textBoxStatus - Read-only TextBox that displays results of the read. 
 
Public Class TestForm 
    Private Sub buttonRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonRead.Click
        If (Not isReadInProgress) Then
            myRmc = RMCLink.CreateEthernetLink(DeviceType.RMC70, "192.168.0.10")
            isReadInProgress = True
 
            ' Disable the Read button and enable the Cancel button.
            buttonRead.Enabled = False
            buttonCancel.Enabled = True
 
            ' Start the Thread that will do the read.
            Dim worker As Thread = New Thread(New ThreadStart(AddressOf doUpdate))
            worker.Name = "Worker"
            worker.Start()
        End If
    End Sub
 
    Private Sub buttonCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonRead.Click
        If (isReadInProgress) Then
            If (Not myRmc Is Nothing) Then
                myRmc.CancelRequest()
            End If
        End If
    End Sub
 
    Private Sub doUpdate()
        Try
            Try
                textBoxStatus.Text = "Connecting..."
 
                myRmc.Connect()
                Try
                    Dim data() As Single = New Single(0) {}
                    textBoxStatus.Text = "Reading..."
                    myRmc.ReadFFile(FileNumber70.fn70StatusAxis0, 8, data, 0, 1)
                    textBoxActPos.Text = String.Format("{0:F3}", data(0))
                    textBoxStatus.Text = "Read successful."
 
                Finally
                    myRmc.Disconnect()
                End Try
 
            Catch ex As ConnectionNotMadeException
                textBoxStatus.Text = "Unable to connect."
                MessageBox.Show("Unable to connect to the controller. " + ex.Message)
 
            Catch ex As ReadWriteFailedException
                textBoxStatus.Text = "Read failed."
                MessageBox.Show("Unable to read from the controller. " + ex.Message)
 
            Catch ex As CancelledException
                textBoxStatus.Text = "Read Cancelled."
            End Try
 
        Finally
            isReadInProgress = False
            buttonRead.Enabled = True
            buttonCancel.Enabled = False
        End Try
 
    End Sub
 
    Private myRmc As RMCLink = Nothing
    Private isReadInProgress As Boolean = False
 
End Class 

 

C#

using System;
using System.Windows.Forms;
using System.Threading;
using RMCLinkNET;
 
// Assumes that the form contains the following controls:
//  buttonRead - Button labeled "Read Position".
//  buttonCancel - Button labeled "Cancel" (defaults disabled).
//  textBoxActPos - Read-only TextBox that displays Axis 0 Actual Position.
//  textBoxStatus - Read-only TextBox that displays results of the read.
 
namespace Example
{
    public partial class TestForm : Form
    {
        ...
 
        private void buttonRead_Click(object sender, EventArgs e)
        {
            if ( ! isReadInProgress)
            {
                myRmc = RMCLink.CreateEthernetLink(DeviceType.RMC70, "192.168.0.10");
                isReadInProgress = true;
 
                // Disable the Read button and enable the Cancel button.
                buttonRead.Enabled = false;
                buttonCancel.Enabled = true;
 
                // Start the Thread that will do the read.
                Thread worker = new Thread(new ThreadStart(doUpdate));
                worker.Name = "Worker";
                worker.Start();
            }
        }
 
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (isReadInProgress)
            {
                if (myRmc != null)
                    myRmc.CancelRequest();
            }
        }
 
        private void doUpdate()
        {
            try
            {
                try
                {
                    textBoxStatus.Text = "Connecting...";
                    myRmc.Connect();
                    try
                    {
                        float[] data = new float[1];
 
                        textBoxStatus.Text = "Reading...";
                        myRmc.ReadFFile((int)FileNumber70.fn70StatusAxis0, 8, data, 0, 1);
                        textBoxActPos.Text = String.Format("{0:F3}", data[0]);
 
                        textBoxStatus.Text = "Read successful.";
                    }
                    finally
                    {
                        myRmc.Disconnect();
                    }
                }
                catch (ConnectionNotMadeException e)
                {
                    textBoxStatus.Text = "Unable to connect.";
                    MessageBox.Show("Unable to connect to the controller. " + e.Message);
                }
                catch (ReadWriteFailedException e)
                {
                    textBoxStatus.Text = "Read failed.";
                    MessageBox.Show("Unable to read from the controller. " + e.Message);
                }
                catch (CancelledException)
                {
                    textBoxStatus.Text = "Read Cancelled.";
                }
            }
            finally
            {
                isReadInProgress = false;
                buttonRead.Enabled = true;
                buttonCancel.Enabled = false;
            }
        }
 
        private RMCLink myRmc = null;
        private bool isReadInProgress = false;
    }
}

 

Visual C++/CLI

namespace RMCLinkNETVCPPSampleUI
{
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Windows::Forms;
    using namespace System::Threading;
    using namespace RMCLinkNET;
 
    // Assumes that the form contains the following controls:
    //  buttonRead - Button labeled "Read Position".
    //  buttonCancel - Button labeled "Cancel" (defaults disabled).
    //  textBoxActPos - Read-only TextBox that displays Axis 0 Actual Position.
    //  textBoxStatus - Read-only TextBox that displays results of the read.
 
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void) : myRmc(nullptr), isReadInProgress(false)
        {
            InitializeComponent();
        }
 
        ...
 
    private:
        System::Void buttonRead_Click(System::Object^  sender, System::EventArgs^ e)
        {
            if ( ! isReadInProgress)
            {
                myRmc = RMCLink::CreateEthernetLink(DeviceType::RMC70, "192.168.0.10");
                isReadInProgress = true;
 
                // Disable the Read button and enable the Cancel button.
                buttonRead->Enabled = false;
                buttonCancel->Enabled = true;
 
                // Start a Thread to do the work.
                Thread^ worker = gcnew Thread(gcnew ThreadStart(this, &Form1::doUpdate));
                worker->Name = "Worker";
                worker->Start();
            }
        }
 
        System::Void buttonCancel_Click(System::Object^  sender, System::EventArgs^ e)
        {
            if (isReadInProgress)
            {
                if (myRmc != nullptr)
                    myRmc->CancelRequest();
            }
        }
 
        System::Void doUpdate(System::Void)
        {
            try
            {
                try
                {
                    textBoxStatus->Text = "Connecting...";
                    myRmc->Connect();
                    try
                    {
                        array<float>^ data = gcnew array<float>(1);
 
                        textBoxStatus->Text = "Reading...";
                        myRmc->ReadFFile((int)FileNumber70::fn70StatusAxis0, 8, data, 0, 1);
                        textBoxActPos->Text = String::Format("{0:F3}", data[0]);
 
                        textBoxStatus->Text = "Read successful.";
                    }
                    finally
                    {
                        myRmc->Disconnect();
                    }
                }
                catch (ConnectionNotMadeException^ e)
                {
                    textBoxStatus->Text = "Unable to connect.";
                    MessageBox::Show("Unable to connect to the controller. " + e->Message);
                }
                catch (ReadWriteFailedException^ e)
                {
                    textBoxStatus->Text = "Read failed.";
                    MessageBox::Show("Unable to read from the controller. " + e->Message);
                }
                catch (CancelledException^)
                {
                    textBoxStatus->Text = "Read Cancelled.";
                }
            }
            finally
            {
                isReadInProgress = false;
                buttonRead->Enabled = true;
                buttonCancel->Enabled = false;
            }
        }
 
        RMCLink^ myRmc;
        bool isReadInProgress;
    };
}

See Also

RMCLink.Interop .NET Assembly | RMCLinkNET Namespace | RMCLink Class


Send comments on this topic.

Copyright (c) 2024 Delta Computer Systems, Inc. dba Delta Motion