Find and share HowTos to various installations / configurations!
kilianvp
Posts: 443 Joined: Fri Jan 16, 2015 10:29 am
CTRL++ callback inside class
Post
by kilianvp » Thu Mar 14, 2019 11:31 am
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 » Wed May 29, 2019 4:04 pm
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 » Mon Jun 03, 2019 3:59 pm
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