Showing results 1 to 3 of 3

Thread: Write variable from background worker

  1. #1
    Join Date

    Default Write variable from background worker

    Dear Forum users!

    I am looking for help with the following problem. I have an application in which I have a background worker in the C# VSTA environment. In this background worker, I repeatedly report progress, which calls the ProgressChanged function of the background worker. In this ProgressChanged function I modify zenon internal variables and write CEL lines with the 
    private static zenOn.IProject myZenonProject;

    myZenonProject.Variables().Item("Variable_name").set_Value(); //and


    functions respectively.
    The problem is, that sometimes these function calls fails and not even an exception is thrown, but the whole Zenon application crashes and stops. The exception that is thrown is either
    "Object reference not set to an instance of an object",
    "Collection was modified, enumeration operation may not execute" or
    "Item with same key has already been added".
    In the Zenon error dump, the exception is "An exception came from the CLR", exception code is 0xE0434F4D.

    I suspected that this error may be a cause of the background worker and running zenon functions in it, so I created a test project, in which I only added a background worker and a zenon variable assignment function call, thus eliminating every other factor. Interestingly the error still occurs, so it is not related to the rest of the application.

    As far as I know, the background worker's ProgressChanged function executes on the main thread. So, I can do whatever, in the backgroundworker, on a different thread, but when I call the ProgressChanged function (through ReportProgress()) it always runs on the main thread, so it shouldn't have thread accessing problems.
    My question is: is this a valid assumption? Is it truly safe to call zenon function in the ProgressChanged function? How could I narrow down the problem, and try to avoid calling functions that cause a crash? What should I check, before running the above mentioned zenon functions, to make sure, that it wont cause problems? If this is not a good practice or valid approach, then what is a good way to handle such tasks? Background worker was the only option I found to handle multiple threads. 

    My environment: Zenon 7.6 SP0 Build56718 Energy Edition
    VSTA 2008 v9.0.30729, .NET 3.5 SP1
    Windows 10 64bit

    Thanks in advance, Peter

  2. #2
    Join Date

    Default Re: Write variable from background worker


    Have you tried to use AddIn Framework?

    I am aware of an error similar to this: The analysis showed that it originates in VSTA - not zenon.
    What could mitigate the problem is to put the COM access behind a semaphore. To ensure that only one thread accesses the COM at any time.

    What you should do is use try catch blocks to catch any exceptions (and log them).
    And use Null-Conditional operators for chaining:


    Last edited by c.f. : 5th July 2021 at 09:02


  3. #3
    Join Date

    Default Re: Write variable from background worker

    Thank you for your reply!

    No, I haven't tried AddIn Framework yet. Do you have any information/documentation about that?

    I have placed the zenon function calls inside a lock statement, and it looks like it has solved the issue, since I can no longer reproduce the error.

    I will implement the Null-Conditional operator as well for additional safety.

    Thanks, for the suggestions!

Similar Threads

  1. Write values to the variable on demand
    By SmartS in forum Engineering Studio & Service Engine (former zenon Editor & Runtime)
    Replies: 0
    Last Post: 29th June 2021, 10:37
  2. Write values to variable is too slow
    By SmartS in forum Engineering Studio & Service Engine (former zenon Editor & Runtime)
    Replies: 0
    Last Post: 15th June 2021, 14:52
  3. To Write Value in Variable with Status
    By rahulindore05 in forum VBA
    Replies: 1
    Last Post: 3rd June 2019, 10:03
  4. Write a complete name of the user to string variable
    By navaneet in forum Engineering Studio & Service Engine (former zenon Editor & Runtime)
    Replies: 2
    Last Post: 14th October 2013, 09:11
  5. Replies: 2
    Last Post: 22nd September 2011, 01:32

Tags for this Thread

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts