1. #1
    AceInfinity's Avatar
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,716

    Queuing Threads from the Generic Queue Class [VB.NET/C# Examples by AceInfinity]

    Here's an example I put together for someone on MSDN who wanted to be able to easily create a queue of methods to start new threads, and be able to start them one after another in order. Thought i'd share it here as well, as this can be very useful for managing a Thread Queue. Thread syncrhonization in an application is valuable, and can get somewhat complex as well...

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            
        End Sub
    
        Private qt As Queue(Of Thread)
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            qt = New Queue(Of Thread)
            qt.Enqueue(New Thread(AddressOf method1))
            qt.Enqueue(New Thread(AddressOf method2))
            qt.Enqueue(New Thread(AddressOf method3))
            qt(0).Start()
        End Sub
    
        Private Sub UpdateQueue()
            qt.Dequeue()
            Sync.Variable = qt.Count
        End Sub
    
        Private Sub method1()
            Console.WriteLine("method1")
            Thread.Sleep(3000)
            UpdateQueue()
        End Sub
    
        Private Sub method2()
            Console.WriteLine("method2")
            Thread.Sleep(3000)
            UpdateQueue()
        End Sub
    
        Private Sub method3()
            Console.WriteLine("method3")
            Thread.Sleep(3000)
            UpdateQueue()
        End Sub
    
        Private WithEvents Sync As New SyncQueueCount
        Private Sub VariableChanged(ByVal QCount As Integer) Handles Sync.VariableChanged
            If QCount > 0 Then
                qt(0).Start()
            Else
                MessageBox.Show("Queue Finished")
            End If
        End Sub
    
    End Class
    
    Public Class SyncQueueCount
        Private S_Int As Integer
        Public Event VariableChanged(ByVal value As Integer)
    
        Public Property Variable() As Integer
            Get
                Variable = S_Int
            End Get
            Set(ByVal value As Integer)
                S_Int = value
                RaiseEvent VariableChanged(S_Int)
            End Set
        End Property
    End Class
    C#: Ignore my project namespace
    Code:
    namespace CSharp_Tester
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                SyncQueue.VariableChanged += new SyncQueueCount._VariableChanged(VariableChanged);
            }
    
            private SyncQueueCount SyncQueue = new SyncQueueCount();
            private Queue<Thread> qt;
            private void button1_Click(object sender, EventArgs e)
            {
                qt = new Queue<Thread>();
                qt.Enqueue(new Thread(method1));
                qt.Enqueue(new Thread(method2));
                qt.Enqueue(new Thread(method3));
                qt.ElementAt(0).Start();
            }
    
            private void UpdateQueue()
            {
                qt.Dequeue();
                SyncQueue.Variable = qt.Count();
            }
    
            private void method1()
            {
                Console.WriteLine("method1");
                Thread.Sleep(3000);
                UpdateQueue();
            }
    
            private void method2()
            {
                Console.WriteLine("method2");
                Thread.Sleep(3000);
                UpdateQueue();
            }
    
            private void method3()
            {
                Console.WriteLine("method3");
                Thread.Sleep(3000);
                UpdateQueue();
            }
    
            private void VariableChanged(int QCount)
            {
                if (QCount > 0)
                    qt.ElementAt(0).Start();
                else
                    MessageBox.Show("Queue Finished");
            }
    
        }
    
    }
    
    public class SyncQueueCount
    {
        private int S_Int;
        public delegate void _VariableChanged(int value);
        public event _VariableChanged VariableChanged;
    
        public int Variable
        {
            get { return S_Int; }
            set {
                S_Int = value;
                VariableChanged(S_Int);
            }
        }
    }
    If anybody here is interested in how this works, just let me know, it was the most basic example I could fit together as a demonstration.

    Note:
    Code:
    Thread.Sleep(3000);
    Thread sleep was put in there to show a noticable delay in which to demonstrate that there is in fact a queued process going on here with starting each thread one after another.
    Last edited by AceInfinity; 05-20-2012 at 10:10 PM.
    \n\n

    Automation Programmer
    Development Site: aceinfinity.net


    • Ad Bot

      advertising
      Beep.

        
       

Similar Threads

  1. .NET Framework
    By Basil in forum General Help & Information
    Replies: 6
    Last Post: 01-10-2013, 01:40 PM
  2. Replies: 2
    Last Post: 09-05-2012, 12:15 AM

Log in

Log in