• matthew@hierthinking.com

Forum

Please or Register to create posts and topics.

Windows service get stuck and spike cpu usage on calling FOCAS cnc_allclibhndl3 function

Hi to everybody!

I made a windows service that each 5 second call the cnc_allclibhndl3 function to get the handle, then call cnc_statinfo/cnc_rddynamic2/cnc_rdsvmeter/cnc_rdspmeter/cnc_sysinfo/cnc_rdparar/cnc_rdalmmsg2, finally release the handle with cnc_freelibhndl.

After running fine for 2-3 hours I notice from the task manager that the service show 100% cpu using.

From the log I can see that the service is stuck on calling the cnc_allclibhndl3 function.

Restarting the service solve the problem for the next 2-3 hours.

The service it's write in C#.

Many thank's to anyone can give usefull tip

I have a follow up on this, I changed the service to leave the handle open until the end of the program or an error from an api call occurred.

Now the service it's stable and it's up and running from some days but I notice a lot of errors on the log calling cnc_statinfo that return -8 "Windows library handle error"

Each time that cnc_statinfo return this error I call cnc_freelibhndl to release the current handle and then cnc_allclibhndl3 to get a new one. With the new handle I can successfully call cnc_statinfo for 2-3 times, with a delay of 5 seconds between each, before I get a -8 error again.

I'm out of ideas, someone have an hint?

I think the reason you are getting the -8 error might be because you didn't release the handle. After you grab the handle, you should release it when you're done. If you hold for another method without quitting, you may get an error of -8.

Or if you are controlling multiple cnc devices, the handles may be overlapping.

Please try and feedback.

Hi Muhammed,

in origin I release the handle after each loop (see first post) but doing that I get the service unresponsive after few hours.

Thank's for your help

I cant help you before see your code.

I can share some of the core logic, this is the code described on the first post

var f = new FanucFocas.Fanuc("ip adress");
await f.StartAsync();

Fanuc class

Focas class

FWLIB32.cs

Kenneth Porter has reacted to this post.
Kenneth Porter

Hi Fabrizio,

Looking at your code, I can see that you are using a Task when executing these calls. The problem with using a Task is that every time the Task is called, it has a chance to be called on a different thread. A Fanuc handle can only be used on the thread it was created on. So if you call the Task 3 times it may execute on the same thread 2 times and then use a different thread the third time, which will invalidate your Fanuc handle. You will need to ensure that every time the Task is executed it is using the same thread or you will continue to run into this issue.

Muhammed Taşdemir and Fabrizio Botarelli have reacted to this post.
Muhammed TaşdemirFabrizio Botarelli

Good to know that handles are locked to threads. Is it safe to use different handles on different threads, eg. one thread per CNC? Or is the Focas internal network state machine locked to a single thread, so all calls for all handles must be on the same thread?

Was this resolved? I see there's a Dispose method so, in principle, the handle should get released at the end of each task run. All the code runs start to finish in one task so there shouldn't be a thread issue. Unless the .Net threading model allows a Task to reschedule execution on different threads behind the scenes. So it might be necessary to use a true thread instead of a task. (Some quick research indicates that Task continuations can use a different thread, so it's not safe to use your Dispose method there.)

Hi Kenneth,

yes I solved the issue moving all the calls (connect - multiple gets - disconnect) inside the same Task.Run

Thank's to Versex for pointing me in the right direction

Kenneth Porter has reacted to this post.
Kenneth Porter