|
-
January 8th, 2009, 05:09 PM
#1
use of lock object
I am writing a dll which runs several threads reading from a Queue, the calling program calls EnqueueTask with a data object. If I use lock around the queue object nothing goes in the queue, if I comment the lock than it works well (but causes a problem with the threads)
Please help
Code:
public class messagesQueue :IDisposable
{
EventWaitHandle waitHandle = new AutoResetEvent (false);
Thread _workerThread;
object locker = new object();
Queue<reportData> _messageQueue;// = new Queue<reportData>();
/// <summary>
/// Constructor
/// </summary>
public messagesQueue()
{
_messageQueue = new Queue<reportData>();
}
/// <summary>
/// Add record to the queue
/// </summary>
/// <param name="reportDataObj"></param>
public void EnqueueTask(reportData reportDataObj)
{
lock (locker)
{
_messageQueue.Enqueue(reportDataObj);
}
waitHandle.Set();
}
-
January 8th, 2009, 06:19 PM
#2
Re: use of lock object
There's nothing wrong with the attached code. The locking is fine. It definitely wouldn't cause the issue you're describing.
If there's a bug, it's in the rest of the code that you didn't paste.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
January 9th, 2009, 01:40 AM
#3
Re: use of lock object
Keep in mind that you need to lock around any code that accesses the queue.
You've shown locking code for Enqueue operations, just make sure there are locks for any other queue operations.
-
January 9th, 2009, 06:21 AM
#4
Re: use of lock object
Just nothing goes and the program continue, or it freeze? And are you sure, that nothing goes into the queue? Maybe something (other thread) is retrieving it from the queue before you spot it's presense.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
January 9th, 2009, 08:24 AM
#5
Re: use of lock object
Nothing happens with the queue, no data is going in. what I found out is that it works if I do Rebuild solution but if I just press F5 than nothing goes on.
-
January 9th, 2009, 08:35 AM
#6
Re: use of lock object
After rebuilding it works, but how do you start the application then??
-
January 9th, 2009, 09:31 AM
#7
Re: use of lock object
If it's randomly working/failing it's quite possible you have a race condition in your code, so post up the rest of the code. Running with the debugger could easily trigger the race as compared to running without. What you're explaining makes no sense at the moment, more info is needed
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
January 9th, 2009, 11:41 AM
#8
Re: use of lock object
After rebuilding I start my application with F5, I will post more code later today.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|