use of lock object
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: use of lock object

  1. #1
    Join Date
    Feb 2002
    Location
    New Jersey
    Posts
    160

    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();
            }

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    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.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,249

    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.

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  5. #5
    Join Date
    Feb 2002
    Location
    New Jersey
    Posts
    160

    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.

  6. #6
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: use of lock object

    After rebuilding it works, but how do you start the application then??

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    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.

  8. #8
    Join Date
    Feb 2002
    Location
    New Jersey
    Posts
    160

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center