CTRL++ callback inside class

Find and share HowTos to various installations / configurations!
3 posts • Page 1 of 1
kilianvp
Posts:443
Joined: Fri Jan 16, 2015 10:29 am

CTRL++ callback inside class

Post by kilianvp »

If i put the callback function inside my class the programm exist immediately after creating DP_Monitor monitor.

Is that the intended behaviour?
In C++ or C# i can put my callback inside my class.

If i add an endless loop with delay after creating DP_Monitor monitor everything works like I expected it.

Code: Select all

class DP_Monitor
{

  public DP_Monitor()
  {
    sysConnect("dpCallback", "dpCreated");
    sysConnect("dpCallback", "dpDeleted");
    sysConnect("dpCallback", "dpTypeCreated");
    sysConnect("dpCallback", "exitRequested");
  }
  
  public dpCallback(string event, mapping data)
  {
    DebugTN(event);
    DebugTN(data);
  }
};

Code: Select all

main()
{
  DP_Monitor monitor = new DP_Monitor();
}

ozangor
Posts:44
Joined: Thu Sep 22, 2011 2:57 pm

Re: CTRL++ callback inside class

Post by ozangor »

Hi there,

You can overcome this issue by having your object as a global variable.
This will keep the object alive as long as the panel/manager runs.

Otherwise, if you just create the instance in the main function, after the scope is gone, then you lose your object. (WinCC OA probably does not interpret it as it has an event loop inside.)

The code below will fix your problem.

Code: Select all

DP_Monitor monitor;

main()
{
  monitor.run();
}
And lastly, I think it is better not to use "new" keyword when not using a shared pointer. The following can be used instead

Code: Select all

DP_Monitor monitor = DP_Monitor(args);

adaneau
Posts:310
Joined: Tue Feb 21, 2012 9:49 am

Re: CTRL++ callback inside class

Post by adaneau »

Hi,

To solve this, I use a global mapping which give more flexibility ;)

mapping mClassPtr; as global variable

and inside init:

mClassPtr["whatevername"] = new myClass;

BR
Alexandre

3 posts • Page 1 of 1