1. Manual locking.
Queue my collection = new Queue();
Lock (myCollection. Synchronization root)
{
Foreach (object item in my collection)
{
//Insert your code here.
}
}
2. Call the Synchronized method, and you will get a thread-safe queue.
Queue mySyncdQ = Queue。 Synchronization (myq);