Solved

Continuous queue

Posted on 2013-01-06
5
273 Views
Last Modified: 2013-01-11
What im trying to do is create a logging queue that i can just enqueue items to when i need to and it will process the items in order of the item been queued. basically i want to be able to call something like
queue.enque("Some Sql query")

Open in new window

and it will then process the SQL query when it has the resources to do so. What i have at the moment is:

The queue:
 Public Class SQLQueueManager
        Public Shared Sqlitems As New Queue

        Public Sub StartProcessing()
            Dim t1 As New Threading.Thread(AddressOf Process)
            t1.Start()
        End Sub

        Public Shared Sub Process()
            Do
                SyncLock Sqlitems.SyncRoot
                    If Sqlitems.Count = 0 Then

                    ElseIf Sqlitems.Count > 0 Then
                        SqlManager.ExecNonQuery(Sqlitems.Peek)
                        Sqlitems.Dequeue()
                    End If
                End SyncLock
            Loop
        End Sub

    End Class

Open in new window


Calling the enqueue:
 Public Shared Sub InsertLog(ByVal dateAndTime As String, ByVal classStr As String, ByVal methordStr As String,
                                    ByVal logType As String, ByVal message As String)
            SQLQueueManager.Sqlitems.Enqueue(
                "Insert log SET DateAndTime =NOW() ,Class ='" & classStr & "' ,Methord ='" & methordStr &
                "' ,LogType ='" & logType & "' ,Message ='" & message & "' ")
        End Sub

Open in new window


Starting the queue:

Dim sqlT As Thread = New Thread(AddressOf SQLQueueManager.Process)
sqlT.Start()

Open in new window


Im not sure if this is the best way to do this, As this is a application that is going to be constantly running and using this queue I'm not sure if it would be up to doing what i need it to do.

Thanks for your help,
Houlahan.
0
Comment
Question by:houlahan
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38748880
You can use the Queue class which is builtin. Then you can start a new thread which runs in the background and monitors the queue constantly. If it finds an item in queue, it processes it otherwise it sleeps for some time.
0
 

Author Comment

by:houlahan
ID: 38749038
I'm not entirely sure what you mean, I've had a look on msdn and i don't really understand how you would initiate this queue and then keep it alive so you can constantly add things to it do you know of any good examples of this?
Thanks
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 166 total points
ID: 38749259
A queue would be just a global level (declared in a module or form level) object of class Queue. You would just access one of its methods Queue/DeQueue and use a separate thread to process it.

http://msdn.microsoft.com/en-us/library/system.collections.queue.aspx
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 167 total points
ID: 38761660
If multiple threads are going to be accessing the queue that CodeCruiser is suggesting, then you are going to want to add proper synchronization to your code since the Queue class is not thread-safe. Otherwise, if you are using .NET 4+, then you can use the ConcurrentQueue.
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 167 total points
ID: 38761703
Another similar approach would be to use a ManualResetEvent() instead of a polling loop with an embedded Sleep().  Whenever an item is queued the ManualResetEvent() could be set allowing the thread to continue and process what was added.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL syntax in VB.net 5 39
VB.Net - Project Software to help the Developer 7 34
Creating a route in asp.net webforms 2 26
VB.NET 2008 - SQL Timeout 9 24
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question