High Priority Thread Getting Blocked

I’m running a high priority logic loop thread in C# every 50ms and it is critical to my application that this thread not get paused/blocked/delayed for greater than 500ms. I’ve got logging in my application so that it will let me know if it ever has any kind of delays and I notice that sometimes at random, it manages to get blocked.

I’ve looked into the services that I’m not using and disabled them, and I still can’t figure out why this is happening. I’ve had one unit running for 40 days with no issues, yet one on my desk will (what seems to be at random) delay my thread, even when the logic is sitting idle and not running any kind of additional processes.

I’ve also noticed that whenever I make changes to the remoteadmin page that the thread gets paused. I’m guessing this is because it’s saving the registry?

Is there something I’m missing, or is it just possible that other processes are allowed to execute for these lengthier times in WinCE?

Dear @mdeiss

The CLR (Common Language Runtime, the environment your C# application runs in) was not designed for real-time processing. There are operations - such as garbage collection - which can kick in and block all application processes for random times. There’s no way to control the priority of C# threads against native threads.

What you should do is to write the critical logic loop in native code, and let it run at a priority level high enough to guarantee your required timing.

Regards, Andy

Makes sense! Thanks for the clarification Andy. I’ve tested by modifying code from the Toradex example on real-time scheduler for WinCE to a fast blinking LED and run other tasks and can’t make it block the blinking LED thread. My background is in electrical engineering and not on computer science, so before I have to do a lot of googling… Is there any way for me to interface all the forms written in C# to my logic and variables that would run in the native code? I highly prefer C# for the ease of programming and was wondering if there would be a simple transition to keep the majority of my C# code. I hate the thought of having to migrate everything to a new set of C++ code.

Any help / recommendations are greatly appreciated.


Dear @mdeiss

I don’t know How your forms need to interact with the control loop, so I can only give some generic hints:

In the simplest case your logic loop is a function that you start once (with function parameters) and let it run.
I guess this is not sufficient for you, so you will probably have one function for the actual loop, and some additional functions to change global variables that your control loop uses as changing input parameters.

C / C++ code

The first step is to create a DLL containing all the functions and global variables mentioned above. Don’t be afraid, this is almost as simple as creating an EXE. Make sure, that you export the relevant functions to make them visible from outside the DLL, by either listing them in the DEF file, or using the compiler hint __declspec( dllexport ).
Even though you can use any complex function parameters, the next step will be easier if you use simple types.

void *Map_MapMemory(DWORD pa, DWORD size);

C# code

The last part is to write the interface, so you can call the C++ functions in the DLL from C#. This is called PInvoke. This is as simple as writing kind of a function prototype along with the DLL name.

public static extern IntPtr Map_MapMemory(UInt32 pa, UInt32 size);

I hope this helps you as a starting point. Please get back if you have more questions.

Regards, Andy