SDK
最新
バナーの背景画像
開発者ガイド
最終更新日 2024年3月23日

カスタム トリガーの作成方法

オートメーション プロジェクトでトリガーの機能を利用できるようになりました。トリガーは、特定のアクションを発生させるためにマシンのアクティビティで特定のイベントがないか監視します。 トリガーは [イベントを監視] フレームワークを使用して設定できますが、このガイドで説明しているようにカスタム トリガーを構築することもできます。

前提条件

カスタム トリガーを作成するには、以下が必要です。

  • Microsoft Visual Studio
  • Microsoft .NET Framework v4.6.1 以降

プロジェクトを作成および構成する

  1. Microsoft Visual Studio を開き、新しいプロジェクトを作成することを選択します。 [プロジェクト] 選択ウィンドウが表示されます。
  2. [ クラス ライブラリ (.NET Framework)]を選択し、[ 次へ] をクリックします。 [新しいプロジェクトを設定 ] ウィンドウが表示されます。
  3. プロジェクト名、場所、ソリューション名、フレームワークを指定します。 .NET Framework 4.6.1 以降のフレームワークを選択してください。すべてのフィールドに入力したら、[ 作成] をクリックします。 新しいプロジェクトが作成され、デザイナー ウィンドウが表示されます。



  4. [ツール ] メニューから [ オプション] を選択します。 [オプション] ウィンドウが表示されます。
  5. [NuGet Package Manager] のエントリを展開し、[ Package Sources] を選択します。
  6. 新しいパッケージ ソースを追加し、[ 名前] フィールドに UiPath オフィシャル フィード を入力し、[ ソース ] フィールドに https://pkgs.dev.azure.com/uipath/Public.Feeds/_packaging/UiPath-Official/nuget/v3/index.jsonを入力します。 [ OK] を クリックして、変更を確定し、保存します。


  7. [ ツール ] メニューをクリックし、[ NuGet Package Manager ] のエントリから [ Solution...] の [NuGet パッケージを管理] を選択します。 [ NuGet - ソリューション ] タブが表示されます。
  8. UiPath Platform の参照を検索して選択します。右側のパネルで、参照を追加するプロジェクトを選択して [ インストール] をクリックします。 [ プレリリースを含める ] チェックボックスをオンにして、 UiPath Platform v20.8 以上の参照をインストールしてください。

トリガー コードを記述する

Once the references are added to the project, it's time to write the code, which should look something like this:

using System; 
using System.Activities; 
using System.Threading; 
using UiPath.Platform.Triggers; 
 
public class TimerTrigger : TriggerBase<TimerTriggerArgs> 
{ 
 //it is recommended to use Variable to store fields in order for  
 //activities like Parallel For Each to work correctly 
   private readonly Variable<Timer> _timer = new Variable<Timer>();   
  public InArgument<TimeSpan> Period { get; set; }    
protected override void CacheMetadata(NativeActivityMetadata metadata) 
    { 
        metadata.AddImplementationVariable(_timer); 
        base.CacheMetadata(metadata); 
    }     
  
//in this method you can subscribe to events. It is called when the trigger starts execution 
protected override void StartMonitor(NativeActivityContext context, Action<TimerTriggerArgs> sendTrigger) 
    { 
        var eventIndex = 0; 
        var period = Period.Get(context); 
        _timer.Set(context, new Timer(OnTick, state: null, dueTime: period, period: period)); 
        return;  
void OnTick(object state) => sendTrigger(new TimerTriggerArgs(eventIndex++));    
}     
 
   //this is used for cleanup. It is called when the trigger is Cancelled or Aborted 
   protected override void StopMonitor(ActivityContext context) => _timer.Get(context).Dispose(); 
} 
 
//Each trigger may declare a type that sub-classes TriggerArgs 
//that corresponds to the “args” item in Trigger Scope activity. If no extra info 
//needs to be passed along, TriggerArgs can be used directly 
public class TimerTriggerArgs : TriggerArgs 
{ 
    public int EventIndex { get; } 
 
    public TimerTriggerArgs(int eventIndex) => EventIndex = eventIndex; 
}using System; 
using System.Activities; 
using System.Threading; 
using UiPath.Platform.Triggers; 
 
public class TimerTrigger : TriggerBase<TimerTriggerArgs> 
{ 
 //it is recommended to use Variable to store fields in order for  
 //activities like Parallel For Each to work correctly 
   private readonly Variable<Timer> _timer = new Variable<Timer>();   
  public InArgument<TimeSpan> Period { get; set; }    
protected override void CacheMetadata(NativeActivityMetadata metadata) 
    { 
        metadata.AddImplementationVariable(_timer); 
        base.CacheMetadata(metadata); 
    }     
  
//in this method you can subscribe to events. It is called when the trigger starts execution 
protected override void StartMonitor(NativeActivityContext context, Action<TimerTriggerArgs> sendTrigger) 
    { 
        var eventIndex = 0; 
        var period = Period.Get(context); 
        _timer.Set(context, new Timer(OnTick, state: null, dueTime: period, period: period)); 
        return;  
void OnTick(object state) => sendTrigger(new TimerTriggerArgs(eventIndex++));    
}     
 
   //this is used for cleanup. It is called when the trigger is Cancelled or Aborted 
   protected override void StopMonitor(ActivityContext context) => _timer.Get(context).Dispose(); 
} 
 
//Each trigger may declare a type that sub-classes TriggerArgs 
//that corresponds to the “args” item in Trigger Scope activity. If no extra info 
//needs to be passed along, TriggerArgs can be used directly 
public class TimerTriggerArgs : TriggerArgs 
{ 
    public int EventIndex { get; } 
 
    public TimerTriggerArgs(int eventIndex) => EventIndex = eventIndex; 
}
次に、 StartMonitor メソッドと StopMonitor メソッドを実装します。 これは、特定のイベントを監視するためのトリガーの動作を決定するために行われます。
イベント ハンドラーでトリガーの使用法を拡張するための引数を指定する場合は、デザイナーに <Custom>TriggerArgs クラスを追加して設定する必要があることに注意してください。

最後の手順として、 ライブラリを構築 し、Studio で使用する NuGet パッケージを作成 します。 この新しいトリガーを [ トリガー スコープ] アクティビティ内で使用できるようになりました。

Was this page helpful?

サポートを受ける
RPA について学ぶ - オートメーション コース
UiPath コミュニティ フォーラム
UiPath ロゴ (白)
信頼とセキュリティ
© 2005-2024 UiPath. All rights reserved.