A straightforward implementation of ISynchronizeInvoke in c#

I ran into an unusual problem the other day…

I’m developing a control that under the hood uses one of three implemented fingerprint scanner API’s in c#. My code is not complicated… just use the relevant DLLs and have their dependencies in my project directory. I have a base scanner class, a single property of that type, and one of the abstract properties of derived classes (ScannerFound) tells the dialog which control it found. It’s like a poor man’s factory pattern – declare a single property of the base type and then iterate and create each derived type found in the assembly; if ScannerFound == true we are done; else try the next one.

But for some reason I cannot fathom, one of the scanners API’s expects a class that implements ISynchronizeInvoke to be passed to its constructor. (Not cool. My Scanner base class is just a plain abstract class and is completely decoupled from the form or control that contains it. I am not passing the Form – which happens to implement ISynchronizeInvoke, to the class, so this means one derived class needs to contain a reference to a class that implements the interface, or implement it itself and in the process add a bunch of unnecessary bullshit to my nice clean class structure.) It’s not an interface I would ever think to implement myself. Why they did this is beyond me because I’d really rather just use an async method or my own thread (if necessary) and don’t need to be tied to the way a Winforms control calls a delegate. In my case, this implementation is only necessary to pass to a constructor, and is used internally in their API by a method I don’t even call. I could probably have used a class that “implements” it by throwing NotImplementedException everywhere but that would just feel wrong.

Anyway, this example I found looks good and I used it as is. To be honest, I’d probably have implemented it myself just for fun if I really cared about it and my code actually used it for more than passing a dummy type, but oh well, I feel almost guilty about not writing here on this blog with the silly header image any more.

Advertisements

About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s