Code sample: Delegating to a queueable Workerthread
public class MyComponent : PAClassAlarmingBase
{
public MyComponent(core.datamodel.ACClass acType, IACObject content, IACObject parentACObject, ACValueList parameter, string acIdentifier = "")
: base(acType, content, parentACObject, parameter, acIdentifier)
{
}
public override bool ACInit(Global.ACStartTypes startChildMode = Global.ACStartTypes.Automatic)
{
bool baseACInit = base.ACInit(startChildMode);
using (ACMonitor.Lock(_20015_LockValue))
{
// 1. Init DelegateQueue
_DelegateQueue = new ACDelegateQueue(this.GetACUrl() + ";Queue");
}
_DelegateQueue.StartWorkerThread();
return baseACInit;
}
public override bool ACDeInit(bool deleteACClassTask = false)
{
// 3. Stop DelegateQueue
_DelegateQueue.StopWorkerThread();
using (ACMonitor.Lock(_20015_LockValue))
{
_DelegateQueue = null;
}
return base.ACDeInit(deleteACClassTask);
}
private ACDelegateQueue _DelegateQueue = null;
public ACDelegateQueue DelegateQueue
{
get
{
using (ACMonitor.Lock(_20015_LockValue))
{
return _DelegateQueue;
}
}
}
public void AnyMethod()
{
// 2. Use DelegateQueue
DelegateQueue.Add(() => { int i = 0; i++; });
// Comparable to:
Task.Run(() => { int i = 0; i++; });
}
}
Modifier: Damir Lisak / Modified:13.12.2020 18:46