Search code examples
c#winformsedsdk

Running EDSDK in a parallel task in C#


This a conceptual question: I want to run the Canon EDSDK in an Windows.Form application. Now ideally, i would like to separate my data from my ui and my model. Specifically this means my windows.form from my EDSDK camera object. By poking around, trying to get the camera to run independently from my form, I came across this posting here EDSDK callbacks not working which basically describes my experiences with the EDSDK initially.

So after further research I saw that I need to start the form with Application.Run() in order to receive the Windows messages for the callbacks. So for the time being, I run the EDSDK within the UI-Form which gives me the functionality I want. I build upon a great toolbox, which can be found here: http://www.codeproject.com/Articles/688276/Canon-EDSDK-Tutorial-in-Csharp. However I've come to experience freezes of my application, most probably due to the system missing the camera callback. (At least this is my assumption)

So what I would like to do is go back to my initial idea to run the EDSDK in a seperate task, thus being able separate causes and debug any issues. I have seen on several other postings that running multiple Application.Run() commands is a bad idea or bad practice. However, I haven't come up with whitty idea how to build the structure of my program differently. I would love to have some input on a better architecture for this purpose.


Solution

  • Author of the linked article here. To make a clean separation you can start a dedicated thread for SDK events. In my commercial library it looks like this:

    • Event loop: STA thread that calls EdsGetEvent (surrounded by a lock). You need to call EdsInitializeSDK on this thread to be recognized as the main thread of the SDK.
    • Camera: STA thread that calls any camera related SDK functions (surrounded by the same lock as the Event loop)
    • SDK Events: will run on the Event Loop thread. You may want to start another thread (threadpool) before invoking the UI (because deadlocks)

    This way you don't have problems with deadlocks and SDK and UI code is completely separated. Of course this also means you have to use a proper thread synchronization to execute functions on each specific thread.

    I'm planing to update the codeproject library with the mentioned changes as soon as I have the time.