Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Iterating and Modifying a .NET Queue?

Iterating and Modifying a .NET Queue?

Scheduled Pinned Locked Moved Visual Basic
questioncsharpmcpdata-structuresperformance
13 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AliAmjad
    wrote on last edited by
    #1

    How can i simultaneously Iterate and modify a .NET Queue i am using the following code:

    Dim q As Queue = New Queue
    
        Sub LoadValues()
            
            q.Enqueue("mystr1")
            q.Enqueue("mystr2")
            q.Enqueue("mystr3")
    
            executeloop()
            
        End Sub
    
        Sub executeloop()
    
            If q.Count > 0 Then
    
                For Each str As String In q
    
                    'do something with str
    
                    q.Dequeue()
    
                    MsgBox(q.Count.ToString())
    
                    Exit For
    
                Next
    
                executeloop()
    
            End If
    
        End Sub
    

    And how can i add items to the Queue while iterating should i use SyncLock because I'm working on a Multithreaded application, and also not sure about the performance need your suggestions... AliAmjad (MCP)

    D 1 Reply Last reply
    0
    • A AliAmjad

      How can i simultaneously Iterate and modify a .NET Queue i am using the following code:

      Dim q As Queue = New Queue
      
          Sub LoadValues()
              
              q.Enqueue("mystr1")
              q.Enqueue("mystr2")
              q.Enqueue("mystr3")
      
              executeloop()
              
          End Sub
      
          Sub executeloop()
      
              If q.Count > 0 Then
      
                  For Each str As String In q
      
                      'do something with str
      
                      q.Dequeue()
      
                      MsgBox(q.Count.ToString())
      
                      Exit For
      
                  Next
      
                  executeloop()
      
              End If
      
          End Sub
      

      And how can i add items to the Queue while iterating should i use SyncLock because I'm working on a Multithreaded application, and also not sure about the performance need your suggestions... AliAmjad (MCP)

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You cannot modify the collection as your enumerating it. You can modify the objects IN the collection, but you cannot modify the size of the collection (Add, Delete) at all in a For/Each loop.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You cannot modify the collection as your enumerating it. You can modify the objects IN the collection, but you cannot modify the size of the collection (Add, Delete) at all in a For/Each loop.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        A Offline
        A Offline
        AliAmjad
        wrote on last edited by
        #3

        Then what should I use for this purpose. I want to add items to the queue and at the same time want to manipulate it. Should I use collection object. And what's wrong with the code given above i know i cannot modify it while iterating but then what should i do to achieve the objective. Thanks for your time man. AliAmjad (MCP)

        D 1 Reply Last reply
        0
        • A AliAmjad

          Then what should I use for this purpose. I want to add items to the queue and at the same time want to manipulate it. Should I use collection object. And what's wrong with the code given above i know i cannot modify it while iterating but then what should i do to achieve the objective. Thanks for your time man. AliAmjad (MCP)

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Why are you using a Queue?? A queue is a list of items that's organized and manipulated on a First-In, First-Out basis. What's the purpose of this collection?? The type of collection you use isn't so much dictated by what it's going to hold, but how the collection is going to be used. If you want to manipulate items, adding them and removing them, then a List(Of T) would work, an array of strings, ArrayList, ...

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          A 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Why are you using a Queue?? A queue is a list of items that's organized and manipulated on a First-In, First-Out basis. What's the purpose of this collection?? The type of collection you use isn't so much dictated by what it's going to hold, but how the collection is going to be used. If you want to manipulate items, adding them and removing them, then a List(Of T) would work, an array of strings, ArrayList, ...

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            A Offline
            A Offline
            AliAmjad
            wrote on last edited by
            #5

            Actually I'm working on a Distributed Web Crawler a Multithreaded application where each URL discovered will going to be added in a queue to implement the breadth-first search model. Many threads will access this queue and get the URLs from the top and then add the newly discovered URLs at the end of the queue and them remove the carwled URLs from the queue. Now what should i use in such a scenario. but when i implemented this logic i get this error "Collection was modified; enumeration operation may not execute". AliAmjad (MCP)

            D 1 Reply Last reply
            0
            • A AliAmjad

              Actually I'm working on a Distributed Web Crawler a Multithreaded application where each URL discovered will going to be added in a queue to implement the breadth-first search model. Many threads will access this queue and get the URLs from the top and then add the newly discovered URLs at the end of the queue and them remove the carwled URLs from the queue. Now what should i use in such a scenario. but when i implemented this logic i get this error "Collection was modified; enumeration operation may not execute". AliAmjad (MCP)

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Why are you enumerating them?? I don't see anything in this description that would justify it. Well, now I see where your code came from. It's a modification of the sample code on MSDN. You don't need a loop to dequeue items. The Dequeue method pops the top item off the queue and returns it an as Object. You have to cast the Object back to a String in order to use it.

              Dim url As String = DirectCast(que.Dequeue(), String)
              

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              A 1 Reply Last reply
              0
              • D Dave Kreskowiak

                Why are you enumerating them?? I don't see anything in this description that would justify it. Well, now I see where your code came from. It's a modification of the sample code on MSDN. You don't need a loop to dequeue items. The Dequeue method pops the top item off the queue and returns it an as Object. You have to cast the Object back to a String in order to use it.

                Dim url As String = DirectCast(que.Dequeue(), String)
                

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007

                A Offline
                A Offline
                AliAmjad
                wrote on last edited by
                #7

                OKKK i get it but as i am in a multithreaded application then lots of threads will access this queue then should i use SyncLock or what else to ensure thread safety. AliAmjad (MCP)

                D 1 Reply Last reply
                0
                • A AliAmjad

                  OKKK i get it but as i am in a multithreaded application then lots of threads will access this queue then should i use SyncLock or what else to ensure thread safety. AliAmjad (MCP)

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Wrap the queue object in a class that implements adding and dequeuing. Use the SyncLock on an object to make it thread safe.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007

                  A 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Wrap the queue object in a class that implements adding and dequeuing. Use the SyncLock on an object to make it thread safe.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007

                    A Offline
                    A Offline
                    AliAmjad
                    wrote on last edited by
                    #9

                    Cooool thanks buddy but I'm going to save MD5 hash algorithm of URLs in hexadecimal form in the Queue so which Queue(of T) i should use so that It'll consume as little memory as possible because I am gonna Enqueue it with lots of URLs. Again thanks for your valuable time Dave. AliAmjad (MCP)

                    D 1 Reply Last reply
                    0
                    • A AliAmjad

                      Cooool thanks buddy but I'm going to save MD5 hash algorithm of URLs in hexadecimal form in the Queue so which Queue(of T) i should use so that It'll consume as little memory as possible because I am gonna Enqueue it with lots of URLs. Again thanks for your valuable time Dave. AliAmjad (MCP)

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      AliAmjad wrote:

                      but I'm going to save MD5 hash algorithm of URLs in hexadecimal form in the Queue so which Queue(of T) i should use so that It'll consume as little memory as possible because I am gonna Enqueue it with lots of URLs

                      That's nice. You better do some research on this first. MD5 is a one-way encryption hash. You can't reverse the hash and get the original URL back. Forget hashing or compressing the URL. It's adding more complexity than you need, and unjustifiably so.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007

                      A 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        AliAmjad wrote:

                        but I'm going to save MD5 hash algorithm of URLs in hexadecimal form in the Queue so which Queue(of T) i should use so that It'll consume as little memory as possible because I am gonna Enqueue it with lots of URLs

                        That's nice. You better do some research on this first. MD5 is a one-way encryption hash. You can't reverse the hash and get the original URL back. Forget hashing or compressing the URL. It's adding more complexity than you need, and unjustifiably so.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007

                        A Offline
                        A Offline
                        AliAmjad
                        wrote on last edited by
                        #11

                        Yes it is a one-way encryption hash and that's why I'll use these for the reference of original URLs saved in a file in this way I'll be able to store many URLs in memory because a web crawler needs to maintain a huge list of URLs in memory and also i'll calculate the MD5 for the page itself for preventing the duplicate entries in index because its size is very small and always equal. So what you suggest on this. Should i go with MD5 or just manipulate raw URLs in memory but this way it will consume lot of memory (RAM). AliAmjad (MCP)

                        D 1 Reply Last reply
                        0
                        • A AliAmjad

                          Yes it is a one-way encryption hash and that's why I'll use these for the reference of original URLs saved in a file in this way I'll be able to store many URLs in memory because a web crawler needs to maintain a huge list of URLs in memory and also i'll calculate the MD5 for the page itself for preventing the duplicate entries in index because its size is very small and always equal. So what you suggest on this. Should i go with MD5 or just manipulate raw URLs in memory but this way it will consume lot of memory (RAM). AliAmjad (MCP)

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #12

                          I don't see the need for MD5 anything in your app. On top of that, with the skill level you've shown in the original post, this shouldn't even be a concern to you right now. Just getting the basic functionality should be. Queue up URL's, dequeue one, download it and move on. THEN you can add complex indexing and hashing to your database.

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                               2006, 2007

                          A 1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            I don't see the need for MD5 anything in your app. On top of that, with the skill level you've shown in the original post, this shouldn't even be a concern to you right now. Just getting the basic functionality should be. Queue up URL's, dequeue one, download it and move on. THEN you can add complex indexing and hashing to your database.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                 2006, 2007

                            A Offline
                            A Offline
                            AliAmjad
                            wrote on last edited by
                            #13

                            Thank you very much man for helping me out I'll surely implement this valuable Logic and analyze the results then I'll tell you about it.

                            Dave Kreskowiak wrote:

                            The skill level you've shown in the original post

                            I consider myself as a beginner because I always try to grasp these important concepts and I've learned allot from you. What do you think about my Skill level? Am i capable enough to take this challenge of making a Distributed Web Crawler although I am up to some degree successfully able to achieve it. But still need a lot of guidance and help form people like you. AliAmjad (MCP)

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups