Net.Pkcs11Interop.HighLevelAPI80.Pkcs11.WaitForSlotEvent C# (CSharp) Method

WaitForSlotEvent() public method

Waits for a slot event, such as token insertion or token removal, to occur
public WaitForSlotEvent ( bool dontBlock, bool &eventOccured, ulong &slotId ) : void
dontBlock bool Flag indicating that method should not block until an event occurs - it should return immediately instead. See PKCS#11 standard for full explanation.
eventOccured bool Flag indicating whether event occured
slotId ulong PKCS#11 handle of slot that the event occurred in
return void
        public void WaitForSlotEvent(bool dontBlock, out bool eventOccured, out ulong slotId)
        {
            if (this._disposed)
                throw new ObjectDisposedException(this.GetType().FullName);

            ulong flags = (dontBlock) ? CKF.CKF_DONT_BLOCK : 0;

            ulong slotId_ = 0;
            CKR rv = _p11.C_WaitForSlotEvent(flags, ref slotId_, IntPtr.Zero);
            if (dontBlock)
            {
                if (rv == CKR.CKR_OK)
                {
                    eventOccured = true;
                    slotId = slotId_;
                }
                else if (rv == CKR.CKR_NO_EVENT)
                {
                    eventOccured = false;
                    slotId = slotId_;
                }
                else
                {
                    throw new Pkcs11Exception("C_WaitForSlotEvent", rv);
                }
            }
            else
            {
                if (rv == CKR.CKR_OK)
                {
                    eventOccured = true;
                    slotId = slotId_;
                }
                else
                {
                    throw new Pkcs11Exception("C_WaitForSlotEvent", rv);
                }
            }
        }

Usage Example

        public void _03_WaitForSlotEventTest()
        {
            if (Platform.UnmanagedLongSize != 8 || Platform.StructPackingSize != 0)
                Assert.Inconclusive("Test cannot be executed on this platform");

            using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.UseOsLocking))
            {
                // Wait for a slot event
                bool eventOccured = false;
                ulong slotId = 0;
                pkcs11.WaitForSlotEvent(true, out eventOccured, out slotId);
                Assert.IsFalse(eventOccured);
            }
        }