Write variable from background worker

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



myZenonProject.Cel().WriteCelStringEx();

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

This is a migrated post! Originally posted on 02.07.2021 by user debaru. Please be aware that information can be outdated.