I am trying to understand a section of code written in C#:
static Semaphore _transactionReceived;
static TransactionsSession _transactionsSession;
static void StartTransactionsStream()
{
WriteNewLine("Starting transactions stream ...");
_transactionsSession = new TransactionsSession(AccountID);
_transactionReceived = new Semaphore(0, 100);
_transactionsSession.DataReceived += OnTransactionReceived;
_transactionsSession.StartSession();
bool success = _transactionReceived.WaitOne(10000);
if (success)
WriteNewLine("Good news!. Transactions stream is functioning.");
else
WriteNewLine("Bad news!. Transactions stream is not functioning.");
}
but I am having trouble understanding what is happening in the code cycle in regards to the Sempahore
class, particularly what the following lines are doing:
_transactionReceived = new Semaphore(0, 100);
and
_transactionReceived.WaitOne(10000)
is doing.
I have viewed and (re)viewed System.Threading.Semaphore
documentation, and I see that the contructor "Initializes a new instance of the Semaphore class, specifying the initial number of entries and the maximum number of concurrent entries." But what does it mean when there are 0
entries?
Additionally, I see that the WaitOne(int32)
call "Blocks the current thread until the current WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval in milliseconds." But again, what does WaitOne
that mean in the context of the code cycle?
Any pointers or general comments about how this is executing would be helpful. Many thanks!