Aveva c# .dll timer/threading problems

 
Parents
  • 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.
Reply
  • 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.
Children
No Data