Timers in C# library
I need to use some Timer functionality in my C# library. C# Timers are not supported in the PLCnext C# framework. Threading also has Timer, but this is also not supported. As a final resort, I am trying to use TimeSpan. This is supported, but does not seem to work. No matter how I initialize or set my TimeSpan, it is always "null".
I have a Function Block with an
[Input, DataType("TIME")]
public uint tPT;
Setting this to T#20s in PLCnext Engineer is successful and I can see that tPT is equal to 20,000 in C# debugger. Now I create
public TimeSpan Preset = TimeSpan.FromMilliseconds(tPT);
The debugger shows Preset ="null". Is there some magic I am missing?
Comments
I was able to create a work around. Since IEC TIME values get passed to C# as UINTs representing milliseconds, I was able to use the DATETIME "Tick" property. There are approximately 10,000 "Ticks" in a millisecond, so you need to normalize the values.
Hi,
here you find more information about supported time, TimeSpan and how to use them.
https://github.com/PLCnext/CSharpExamples/blob/master/PLCnext_CSharpExamples/09_DateTime/FBWithDateTime.cs
Regards
Marcel
Hi Marcel,
Thank you for your response. I did some additional testing and my issue is related to the Microsoft C# debugger. TimeSpan is a STRUCT and has several members. For some reason the VS 2017 debugger shows it as a VAR with a value of NULL. With this knowledge I was able to get my timer function working with TimeSpan.
Thank you again for your help.
Thank you,
Loren