Aveva c# .dll timer/threading problems

 
  • Hi!

    Using any sort of timers (threading, timer) in .dll for aveva seems to crash aveva after sometime, can be first one or sometimes takes several minutes with 15 second timer.
    Anyone else experiencing problems with aveva and c# timers and if not how did you do it?
    Any assist on how you solved timer stuff with c# on aveva would be great.

    Few of the things i tried:

     
    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 something like this in .dll causes it to crash after some time. Am I doing something wrong or missing something?
    Also tried using timers and just worker thread with same crashy results after a while:


    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);
                   }
               }
           }
    }


    Thanks for any help!
  • Hello Metzger !

    According to this link:
    https://help.aveva.com/AVEVA_Everything3D/3.1/wwhelp/wwhimpl/js/html/wwhelp.htm#href=NCUG/NCUG2.03.14.html


    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.


    When you use multithreading in an Aveva context, I recommend you to execute in main thread (the Aveva thread) all functions that have an interaction with Aveva environment (databases, UI...).

    For your Timer example, a solution could be to execute the Timer event in the main Thread (by default another thread is used).
    Here is a solution that I used using a System.Timers.Timer:


    Timer timer = new Timer();
    timer.SynchronizingObject = (ISynchronizeInvoke)this;
    timer.Elapsed += Timer_Elapsed;
    timer.Interval = 1000;
    timer.Start();

    Where this, in my case, was a UserControl.

    More generally I also use Dispatchers:
    https://docs.microsoft.com/fr-fr/dotnet/api/system.windows.threading.dispatcher?view=netcore-3.1

    By retrieving, when starting my application, the dispatcher linked to Aveva's thread:

    Dispatcher avevaDispatcher = Dispatcher.CurrentDispatcher;


    And run the functions from another threads through this dispatcher:

    avevaDispatcher.BeginInvoke(new Action(() => MyMethod()));

    With this solution, MyMethod(), that interacts with Aveva DBs for example, will be executed in the Aveva thread, from a parallel thread.
  • Thanks for the help L.
    I think the real solution for my case is most likely the dispatcher.
    Even thou the event I'm calling with the timer has nothing to do aveva or is even callable by aveva, I do run other aveva NON timer stuff that can be called in the same Main.
    I will test this stuff out.
    I have never used a dispatcher but I assume it's something like a background worker.
    So in this case if I have aveva stuff in main should I run the timer related things in the dispatcher?
  • I think I got it working.
    Thanks for all the help L.