Discussion about recent product features & solutions!
3 posts • Page 1 of 1
3 posts
• Page 1 of 1
johna_ssa
Posts:21
Joined: Wed Jan 28, 2015 6:14 pm
Implementing a dp compatible non-blocking timer
Postby johna_ssa »
In an effort to give back instead of always asking questions, I figured I would share a couple simple functions I have been using. It's possible there is already a dp function that can do this. But, I couldn't find it.
I have implemented a very simple non-blocking timer (similar to a call back) using a dp. I use this for time-out timers, timed notifications, etc. It encapsulates the dpSet function pushing it into a new thread with a delay. This simple little function makes implementing timers easy.
// Non blocking dpSet call delayed by given seconds
// Returns an integer that is the handle to the thread created for the timer
// instance. "Handle" is need if you plan to call dpSetStop to stop the timer
// prior to it actually setting the DP.
int dpSetDelayed(int secs, string dp, anytype val)
{
return(startThread("dpSetDelayed_thread",secs,dp,val));
}
// Stop a delayed dpSet created by a prior call to dpSetDelayed
// "handle" is the integer value returned by the previous call to dpSetDelayed
// Returns 0 is the stopThread call succeeded. Returns -1 if the stopThread
// call failed (ie the delay completed and the dpSet has aleady been
// implemented).
int dpSetStop(int handle)
{
return(stopThread(handle));
}
// This is the thread that implements the delay and dpSet
void dpSetDelayed_thread(int secs, string dp, anytype val)
{
delay(secs);
dpSet(dp,val);
}
If you have improvements or even a better way to do this with existing functions, please share it here.
Re: Implementing a dp compatible non-blocking timer
Postby leoknipp »
I do not know when these functions are used in your project and what the conditions are to stop the thread.
If you want to wait for a given time and make a check based on conditions you can use the function dpWaitForValue().
At this function you can define a timeout, dp elements for the conditions, the values which are used for the conditions and dp elements for the result values.
For further information please have a look at the WinCC OA documentation.
Best Regards
Leopold Knipp
Senior Support Specialist
Re: Implementing a dp compatible non-blocking timer
Postby RudiKreiner »
See also the control function timedFunc() in the WinCC Help. After figuring out how to configure it (it has a wide range of operating modes), I used it in several application where I needed non-blocking timers. It seems to me to be more elegant than a thread with a delay() in it.