private void WaitChangeStatus(object sender, DoWorkEventArgs e) { while (!e.Cancel) { SmartCardErrors result; // Obtain a lock when we use the context pointer, which may be modified in the Dispose() method. lock (this) { if (context == IntPtr.Zero) { return; } // This thread will be executed every 1000ms. The thread also blocks for 1000ms, meaning that the application // may keep on running for one extra second after the user has closed the Main Form. result = GetStatusChange(context, 1000, states, readers.Count); } if (result == SmartCardErrors.SCARD_E_TIMEOUT) { // Time out has passed, but there is no new info. Just go on with the loop continue; } CheckResult(result); for (int i = 0; i < states.Length; i++) { // Check if the state changed from the last time if ((states[i].EventState & CardState.Changed) == CardState.Changed) { // Check what changed. SmartCardState state = SmartCardState.None; if ((states[i].EventState & CardState.Present) == CardState.Present && (states[i].CurrentState & CardState.Present) != CardState.Present) { // The card was inserted. state = SmartCardState.Inserted; } else if ((states[i].EventState & CardState.Empty) == CardState.Empty && (states[i].CurrentState & CardState.Empty) != CardState.Empty) { // The card was ejected. state = SmartCardState.Ejected; } if (state != SmartCardState.None && states[i].CurrentState != CardState.Unaware) { // Raise an event to indicate the change, if we weren't previously unaware of the state // This prevents from the event being raised the first time. this.OnStateChanged( new SmartCardStateChangedEventArgs( states[i].Reader, state, states[i].rgbAtr)); } // Update the current state // for the next time they are checked. states[i].CurrentState = states[i].EventState; } } } }
public void WaitForState(SmartCardState state, int timeout) { SmartCardState retState; int ret = MCardWaitForCardState(context, state, out retState, (uint)timeout); if (ret != 0) { throw new Exception(ret.ToString("X")); } }
public void WaitForStatusChange(string reader, SmartCardState currentState, SmartCardState newState, long timeout) { if ((timeout < 0) || (timeout > uint.MaxValue)) { throw new ArgumentOutOfRangeException("timeout must be a 32-bit unsigned integer value"); } ReaderState readerState = new ReaderState(); readerState.Reader = reader; readerState.UserData = IntPtr.Zero; readerState.CurrentState = currentState; readerState.EventState = newState; readerState.AtrLength = 0; int ret = SCardGetStatusChange(context, (uint)timeout, ref readerState, 1); if (ret != 0) { throw ToException(ret); } }
private static extern int MCardWaitForCardState(IntPtr context, SmartCardState expectedCardState, out SmartCardState cardState, uint timeout);
public void WaitForState(SmartCardState state, int timeout) { SmartCardState retState; int ret = MCardWaitForCardState(context, state, out retState, (uint)timeout); if (ret != 0) throw new Exception(ret.ToString("X")); }
private static extern int SCardGetStatus(IntPtr card, IntPtr readerName, ref uint readerLength, out SmartCardState state, out SmartCardProtocols protocol, IntPtr atr, out uint atrLength);
public SmartCardStateChangedEventArgs(string reader, SmartCardState state, byte[] atr) { this.reader = reader; this.state = state; this.atr = atr; }
public void WaitForStatusChange(string reader, SmartCardState currentState, SmartCardState newState) { WaitForStatusChange(reader, currentState, newState, uint.MaxValue); }
public void WaitForStatusChange(string reader, SmartCardState currentState, SmartCardState newState, long timeout) { if ((timeout < 0) || (timeout > uint.MaxValue)) throw new ArgumentOutOfRangeException("timeout must be a 32-bit unsigned integer value"); ReaderState readerState = new ReaderState(); readerState.Reader = reader; readerState.UserData = IntPtr.Zero; readerState.CurrentState = currentState; readerState.EventState = newState; readerState.AtrLength = 0; int ret = SCardGetStatusChange(context, (uint)timeout, ref readerState, 1); if (ret != 0) throw ToException(ret); }