构造函数:LongRunningTask(action, executingNode)

LongRunningTask
任务运行有时间限制或 CPU 限制的代码。
LongRunningTask(Action action, IUANode executingNode);
小贴士: 将
LongRunningTask
用于长期运行或阻塞性操作,尤其是在代码必须访问项目节点时。保持生命周期方法简短,并尽可能将扩展工作移出
Start()
。有关线程处理的更多信息,请参见运行时线程处理模型。

参数

action
(Action)
要运行的方法或 lambda 表达式。
executingNode
(IUANode)
在其中运行代码的节点。

示例

myLongRunningTask
任务将使用
ProcessCsvFile()
方法处理 CSV 文件。此方法将任务本身作为参数,在读取 CSV 文件的每一行后都使用
IsCancellationRequested
属性检查该参数的状态。可以通过这种方法取消任务。
using System.IO; // For using the StreamReader public class RuntimeNetLogic1 : BaseNetLogic { public override void Start() { // Insert code to be executed when the user-defined logic is started myLongRunningTask = new LongRunningTask(ProcessCsvFile, LogicObject); myLongRunningTask.Start(); } public override void Stop() { // Insert code to be executed when the user-defined logic is stopped myLongRunningTask.Dispose(); } private void ProcessCsvFile(LongRunningTask task) { // example method to read lines from a csv file using (var reader = new StreamReader("
path/to/csv/file.csv
")) { while (!reader.EndOfStream) { // Check whether task cancellation has been requested if (task.IsCancellationRequested) { // Properly handle task cancellation here return; } string line = reader.ReadLine(); // Process line } } } private LongRunningTask myLongRunningTask; }
提供反馈
对本文档有问题或反馈吗? 请在这里提交您的反馈 。
Normal