Ejemplo n.º 1
0
        /// <summary>
        /// Lock the underlying driver.
        ///
        /// If the underlying driver requires a "key" (such as VISA's viLock()), use the
        /// value returned by LockSession() of the first session locked.  If this is the
        /// first call to LockSession for this shared session, the value is the suggested
        /// value for the key (but the implementation may ignore this and return a
        /// different value).
        /// </summary>
        /// <param name="exclusive">true (VI_EXCLUSIVE_LOCK), false (VI_SHARED_LOCK)</param>
        /// <param name="timeout">timeout in ms ... not all drivers/sessions use this</param>
        /// <param name="key">"authorization" to lock this visa session.</param>
        /// <returns>authorization for other calls to LockSession of a shared session</returns>
        public string LockSession(bool exclusive, int timeout, string key)
        {
            // If not open, report an error...
            if (mSession == 0)
            {
                throw new IOException("Cannot lock a closed VisaSession.");
            }

            const int VI_ERROR_TMO         = -1073807339; // = 0xBFFF0015
            const int VI_ERROR_RSRC_LOCKED = -1073807345; // = 0xBFFF000F

            StringBuilder buffer = new StringBuilder(256);
            int           error  = AgVisa32.viLock(mSession,
                                                   (exclusive) ? AgVisa32.VI_EXCLUSIVE_LOCK : AgVisa32.VI_SHARED_LOCK,
                                                   timeout,
                                                   key,
                                                   buffer);

            if (error < 0)
            {
                // If someone else has this locked, we get a timeout ... "translate"
                // that error into "resource locked"
                if (error == VI_ERROR_TMO)
                {
                    error = VI_ERROR_RSRC_LOCKED;
                }
                AgVisa32Exception.Throw(error);
            }
            IsSessionLocked = true;
            return(buffer.ToString());
        }