C# – Device Driver DLL Blocking vs NonBlocking

blockingcdrivermultithreadingnonblocking

My company makes a product that connect to the PC via USB. I am writing a DLL driver, using Visual C#, for this product so that anyone who wants to write a program that can control or device can do so. Some of the operations that the driver will execute take several seconds for the device to complete (for example moving the motor in the device 10,000 steps). I am considering two different approaches to this driver and I'm not sure which is ideal.

First approach: Make the commands Non-blocking. Basically the client application would call a method in my dll to start the motor turning and that method would immediately return. Then I would have an Event, 'MoveFinished' that is triggered when the move is finished. This approach would allow the client application to not freeze-up and do other operations while the motor is moving without using multi-threading.

Second approach: Make the commands blocking. So when the client application calls the Move method, the method blocks and doesn't return until the move is complete. This would cause the client application to freeze while the move is taking place (unless they use multi-threading) but it could also be useful in that it won't allow the client application to make dumb mistakes like calling the move method while the device is already moving…

Does anyone have any experience, wisdom, comments or thoughts to share?

Best Answer

I would do both. Since you are writing an API into your device I would give the final user the flexibility to decide how they want the method to operate. Either Sync or Async.

If you can only choose one route I would favor Async. The reason is that most users developing against the device would most likely not like there application to freeze while the device is completing a process.

The event structure you are talking about is good.