using System.Threading;
public class SomeClass
{
private int Set_delay = 15;
private System.Threading.Timer TTimer;
private bool TimeStop = true;
Start();
private void TickTimer(object state)
{
DoStuff();
if (TimeStop == false) Thread.Sleep(Set_delay * 1000);
}
private void Start()
{
TimeStop = false;
TTimer = new System.Threading.Timer(new TimerCallback(TickTimer), null, Set_delay * 1000, Set_delay * 1000);
}
}
using System.Threading;
public class SomeClass
{
private int Set_delay = 15; private void Start()
{
Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
thread.Start();
}
public void WorkThreadFunction()
{
bool set = true;
DateTime TimeStamp = DateTime.Now;
while (set)
{
if (DateValidity(TimeStamp) == false)
{
DoStuff();
TimeStamp = TimeStamp.AddSeconds(Set_delay);
}
}
}
}
C# allows code to use multiple threads and there are situations where this can be useful in an Addin. However this technique should be used with care as the host AVEVA program is not itself multiple thread safe. Thus there can be only one thread at a time which executes functions in DbLayer, CAF or other host core code.
Timer timer = new Timer();
timer.SynchronizingObject = (ISynchronizeInvoke)this;
timer.Elapsed += Timer_Elapsed;
timer.Interval = 1000;
timer.Start();
Dispatcher avevaDispatcher = Dispatcher.CurrentDispatcher;
avevaDispatcher.BeginInvoke(new Action(() => MyMethod()));