I am getting a stack empty exception. How is that possible if the stack is not empty (it has 16 items)?
I got a snap shot of the error:
Can someone please explain?
I am getting a stack empty exception. How is that possible if the stack is not empty (it has 16 items)?
I got a snap shot of the error:
Can someone please explain?
If
SharedMemory
is aStack
, and since you are using Multithreading and if you are on .Net 4 . you should use : ConcurrentStackEdit
After my first edit and a great comment from Quartermeister this a simpler working solution:
You must synchronize access when using something like
Stack<T>
. The simplest approach is to uselock
, which then also let's you use thelock
for the synchronization itself; so pop would be:and push would be:
In multithreading environment it is very much possible.
Are you using more than one threads in your program? If yes,
SharedMemory
should belock
ed before making any change to it.