Click here to Skip to main content
15,905,238 members
Home / Discussions / C#
   

C#

 
QuestionLinq Noob, stuck on type inference... Pin
callingshotgun18-Aug-08 18:54
callingshotgun18-Aug-08 18:54 
AnswerRe: Linq Noob, stuck on type inference... Pin
leppie18-Aug-08 22:14
leppie18-Aug-08 22:14 
GeneralRe: Linq Noob, stuck on type inference... Pin
callingshotgun19-Aug-08 5:06
callingshotgun19-Aug-08 5:06 
GeneralRe: Linq Noob, stuck on type inference... Pin
leppie19-Aug-08 5:47
leppie19-Aug-08 5:47 
GeneralRe: Linq Noob, stuck on type inference... Pin
callingshotgun19-Aug-08 6:06
callingshotgun19-Aug-08 6:06 
QuestionPInvoke Heartburn Pin
ahfong18-Aug-08 14:33
ahfong18-Aug-08 14:33 
AnswerRe: PInvoke Heartburn Pin
leppie18-Aug-08 22:08
leppie18-Aug-08 22:08 
QuestionShould GenericQueue.Enqueue work while a different thread has LOCK(GenericQueue) ? Pin
JoeRip18-Aug-08 11:46
JoeRip18-Aug-08 11:46 
I have a Generic Queue<privateType> object, myGenericQueue.

My application receives events from another application. In my event handler, I asynchronously (and therefore on a different thread) enqueue data to myGenericQueue, using a delegate (myDelegate.beginInvoke).

I use a callback method to determine if the Enqueue operation succeeded. Both the asynch method and the callback produce debug output.

In my app's main(), a Form Timer periodically pings, at which time I LOCK myGenericQueue, enumerate and dequeue it. Again, more debug output.

To test for synchronization problems, I put a long sleep in my dequeue loop, so that myGenericQueue will be locked when my event handlers try to Enqueue data.

Here's what I'm seeing, that I don't understand: while myGenericQueue is theoretically LOCKed, my event handlers are still having no problems Enqueuing data. The callbacks fire right away, suggesting no delay, and all of the debug output matches order/timing expectations. All data is making it to myGenericQueue.

Is this expected? Why is there no collision with the lock?

The documentation for the Generic Queue object says:

A Queue<T> can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

Doesn't this suggest I should be seeing collisions?

Here's my dequeuing code:

lock (myGenericQueue)
{
     while (myGenericQueue.Count > 0)
     {
     // copy items from queue to List
     myList.Add((privateType)myGenericQueue.Dequeue());

     // REMOVE: DEBUG: HACK: TODO: remove this, testing only!
     System.Diagnostics.Debugger.Log(0, "", "Starting 5 sec sleep at: " + DateTime.Now.Ticks + Environment.NewLine);
     System.Threading.Thread.Sleep(5000);
     System.Diagnostics.Debugger.Log(0, "", "Coming out of 5 sec sleep at: " + DateTime.Now.Ticks + Environment.NewLine);
     }
}

AnswerRe: Should GenericQueue.Enqueue work while a different thread has LOCK(GenericQueue) ? Pin
Colin Angus Mackay18-Aug-08 12:41
Colin Angus Mackay18-Aug-08 12:41 
GeneralRe: Should GenericQueue.Enqueue work while a different thread has LOCK(GenericQueue) ? Pin
JoeRip18-Aug-08 14:19
JoeRip18-Aug-08 14:19 
GeneralRe: Should GenericQueue.Enqueue work while a different thread has LOCK(GenericQueue) ? Pin
Colin Angus Mackay18-Aug-08 21:06
Colin Angus Mackay18-Aug-08 21:06 
GeneralRe: Should GenericQueue.Enqueue work while a different thread has LOCK(GenericQueue) ? Pin
JoeRip18-Aug-08 21:12
JoeRip18-Aug-08 21:12 
AnswerRe: Should GenericQueue.Enqueue work while a different thread has LOCK(GenericQueue) ? Pin
Guffa18-Aug-08 22:00
Guffa18-Aug-08 22:00 
GeneralRe: Should GenericQueue.Enqueue work while a different thread has LOCK(GenericQueue) ? Pin
JoeRip18-Aug-08 22:07
JoeRip18-Aug-08 22:07 
AnswerRe: Should GenericQueue.Enqueue work while a different thread has LOCK(GenericQueue) ? Pin
Guffa18-Aug-08 12:41
Guffa18-Aug-08 12:41 
Question[Message Deleted] Pin
JoeRip18-Aug-08 11:12
JoeRip18-Aug-08 11:12 
AnswerRe: Why doesn't Queue.Enqueue fail when Queue is locked? Pin
led mike18-Aug-08 11:22
led mike18-Aug-08 11:22 
QuestionCode To Check Email Errors Pin
Kevin Marois18-Aug-08 10:56
professionalKevin Marois18-Aug-08 10:56 
AnswerRe: Code To Check Email Errors Pin
Manas Bhardwaj18-Aug-08 12:23
professionalManas Bhardwaj18-Aug-08 12:23 
QuestionAssigning an ArrayList, i get a NullReferenceException Pin
AAKAra18-Aug-08 10:52
AAKAra18-Aug-08 10:52 
AnswerRe: Assigning an ArrayList, i get a NullReferenceException Pin
Ennis Ray Lynch, Jr.18-Aug-08 10:58
Ennis Ray Lynch, Jr.18-Aug-08 10:58 
AnswerRe: Assigning an ArrayList, i get a NullReferenceException Pin
DaveyM6918-Aug-08 12:47
professionalDaveyM6918-Aug-08 12:47 
AnswerRe: Assigning an ArrayList, i get a NullReferenceException Pin
Guffa18-Aug-08 12:59
Guffa18-Aug-08 12:59 
Questionhow kill a thread that is on wait state?????? Pin
mr.mohsen18-Aug-08 9:29
mr.mohsen18-Aug-08 9:29 
AnswerRe: how kill a thread that is on wait state?????? Pin
Mark Salsbery18-Aug-08 9:45
Mark Salsbery18-Aug-08 9:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.