# Monkey testing a SmartCard library

My definition of monkey testing is basically playing with a program in as if I was a monkey (press every button, unplug things, go in the wrong order..etc etc)

So I made a rather simple SmartCard library that when I "enable" it it just polls the smart card reader for a card, and reports back information. see code

private void CardStatusChange()
{
while (run)
{
if (SCWrapper.SCardGetStatusChange(scHandle, UInt32.MaxValue, readerState, 1) == SCardFunctionReturnCodes.SCARD_S_SUCCESS)
{
}
}
}

private void SendCardStatus(SCardState sCardState)
{
//case SCardState.SCARD_STATE_UNAWARE:
//case SCardState.SCARD_STATE_IGNORE:
//case SCardState.SCARD_STATE_CHANGED:
//case SCardState.SCARD_STATE_UNKNOWN:
//case SCardState.SCARD_STATE_UNAVAILABLE:
//case SCardState.SCARD_STATE_EMPTY:
OnCardUnplugged(); //fire event
//case SCardState.SCARD_STATE_PRESENT:
{
OnCardPluggedIn(); //fire event
}
//case SCardState.SCARD_STATE_ATRMATCH:
//case SCardState.SCARD_STATE_EXCLUSIVE:
//case SCardState.SCARD_STATE_INUSE:
//case SCardState.SCARD_STATE_MUTE:
//case SCardState.SCARD_STATE_UNPOWERED:
}

{
}


This part works as expected in that while my thread is running I can plug in and unplug a smartcard as often and as quickly as I want without problem. What I'm struggling with is the Threading portion of it. Visual Studio screams at me for using suspend and I found a bug when using suspend in my thread so I am taking that out (in other words don't comment on that) but this is how I "open" and "enable" my smart card

public override bool Open()
{
//code here to setup the smart card
return true;
}

public override bool Enable()
{
run = true;
//thread start checking for card events
{//this is probably a mistake
break;
break;
break;
break;
break;
break;
break;

break;

break;
break;
default:
break;
}

return true;
}
public override void Disable()
{
run = false;
}


I've never been too good at threading, but I do need this as a thread (I think I do atleast I am open to other ideas). So how should I really Enable and Disable? I used to just set run to true or false, but my monkey testing showed that this doesn't always work and sometimes Windows would cry that it can't start a running thread... SO I dunno what to do. Any thoughts?

• I think I found the answer. It appears to be work. On the Disable instead of readerThread.Suspend() I change it to readertThread.Join(). Then I just check for the Unstarted flag for Enable and fire it up. So far it is working good with no hicups. still have further testing. – Robert Snyder Apr 19 '13 at 21:21
• Can you post the answer you found as an answer? This will help future users. – Ryan Gates Jun 14 '13 at 15:36
• @RyanGates Sure can... :) – Robert Snyder Jun 14 '13 at 16:07

As requested by Ryan Gates I should post my results. I don't remember the entire scope of what i did, but I do know where the code is :)

So the basics are that I want the thread to be able to exit without causing any strange hicups if the user decides to use my library incorrectly. So I made the thread turn into a background thread. I was able to remove the ugly switch statement and just check the appropriate bits are set to make sure I can start my thread (basically only allows one thread to read a smart card instead of multiple reading the same card)

On Disable what I did was set my flag to kill the loop in my readerThread thread. I then wait for it to exit by using the thread.Join Clear as mud??? I can try to clear up more if anyone has questions.

public override bool Open()
{
//Code to setup SmartCard

return true;
}
public override bool Enable()
{
run = true;
//thread start checking for card events