Ejemplo n.º 1
0
        /// <summary>
        /// Initializes the SDK
        /// </summary>
        /// <param name="useCallingThread">If true, the calling thread will be used as SDK main thread;
        /// if false, a separate thread will be created</param>
        public CanonAPI(bool useCallingThread)
        {
            try
            {
                //Ensure that only one caller at a time can increase the counter
                lock (InitLock)
                {
                    //If no instance exists yet, initialize everything
                    if (RefCount == 0)
                    {
                        if (useCallingThread)
                        {
                            if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
                            {
                                throw new ThreadStateException("Calling thread must be in STA");
                            }
                            ErrorHandler.CheckError(this, CanonSDK.EdsInitializeSDK());
                        }
                        else
                        {
                            //Trying to trigger DllNotFoundException so it's not thrown
                            //in the event loop on a different thread:
                            CanonSDK.EdsRelease(IntPtr.Zero);

                            //Start the main thread where SDK will run on
                            MainThread = new ApiThread();
                            MainThread.Start();
                            //Initialize the SDK on the main thread
                            MainThread.Invoke(() => ErrorHandler.CheckError(this, CanonSDK.EdsInitializeSDK()));
                        }

                        CanonSDK.InitializeVersion();
                        //Subscribe to the CameraAdded event
                        CameraAddedEvent = new SDKCameraAddedHandler(CanonAPI_CameraAddedEvent);
                        ErrorHandler.CheckError(this, CanonSDK.EdsSetCameraAddedHandler(CameraAddedEvent, IntPtr.Zero));
                        _IsSDKInitialized = true;
                    }
                    RefCount++;
                }
            }
            catch (Exception e)
            {
                IsDisposed = true;
                if (MainThread?.IsRunning == true)
                {
                    MainThread.Shutdown();
                }
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Terminates the SDK and disposes resources
        /// </summary>
        /// <param name="managed">True if called from Dispose, false if called from the finalizer/destructor</param>
        protected virtual void Dispose(bool managed)
        {
            //Ensure that only one caller at a time can decrease the counter
            lock (InitLock)
            {
                if (!IsDisposed)
                {
                    //If it's the last instance, release everything
                    if (RefCount == 1)
                    {
                        _IsSDKInitialized = false;//Set beforehand because if an error happens, the SDK will be in an unstable state anyway

                        //Remove event handler for the CameraAdded event
                        ErrorCode err = CanonSDK.EdsSetCameraAddedHandler(null, IntPtr.Zero);
                        if (managed)
                        {
                            ErrorHandler.CheckError(this, err);
                            //Dispose all the connected cameras
                            CurrentCameras.ForEach(t => t.Dispose());
                        }
                        //Terminate the SDK
                        if (MainThread?.IsRunning == true)
                        {
                            err = MainThread.Invoke(() => { return(CanonSDK.EdsTerminateSDK()); });
                        }
                        //Close the main thread
                        if (MainThread?.IsRunning == true)
                        {
                            MainThread.Shutdown();
                        }
                        if (managed)
                        {
                            ErrorHandler.CheckError(this, err);
                        }
                    }
                    RefCount--;
                    IsDisposed = true;
                }
            }
        }