Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 84 - Real time example of queue collection class in c#

Suggested Videos 
Part 81 - When to use a dictionary over list in c#
Part 82 - Generic Queue collection class
Part 83 - Generic stack collection class



Here is one simple real-time working example where a Queue class can be used.

When you walk into a bank or a passport office, you will collect a token and wait in the queue for your token number to be called. From the application perspective, when a token is issued, the token number will be added to the end of the Queue. When a representative at the counter is available to server a customer, he will push the "Next" button and the token number that is present at the beginning of the queue, will be dequeued. So, this is one example, where a Queue collection class can be effectively used.
Real time example of queue collection class in c#



WebForm1.aspx
<table style="border:1px solid black; font-family:Arial; text-align:center">
    <tr>
        <td>
            <b>Counter 1</b>
        </td>
        <td>
            <b>Counter 2</b>
        </td>
        <td>
            <b>Counter 3</b>
        </td>
    </tr>
    <tr>
        <td>
            <asp:TextBox ID="txtCounter1" Width="150px" Font-Size="Large" runat="server"
                            BackColor="#000099" ForeColor="White">
            </asp:TextBox>
        </td>
        <td>
            <asp:TextBox ID="txtCounter2" Width="150px" Font-Size="Large" runat="server"
                            BackColor="#000099" ForeColor="White">
            </asp:TextBox>
        </td>
        <td>
            <asp:TextBox ID="txtCounter3" Width="150px" Font-Size="Large" runat="server"
                            BackColor="#000099" ForeColor="White">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="btnCounter1" Width="150px" runat="server" Text="Next" 
                onclick="btnCounter1_Click" />
        </td>
        <td>
            <asp:Button ID="btnCounter2" Width="150px" runat="server" Text="Next" 
                onclick="btnCounter2_Click" />
        </td>
        <td>
            <asp:Button ID="btnCounter3" Width="150px" runat="server" Text="Next" 
                onclick="btnCounter3_Click" />
        </td>
    </tr>
    <tr>
        <td colspan="3">
            <asp:TextBox ID="txtNextToken" Font-Size="Large" Width="500px" runat="server"
                        BackColor="#003300" ForeColor="White">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="3">
            <asp:ListBox ID="listTokens" Width="100px" Font-Size="Medium" runat="server">
            </asp:ListBox>
        </td>
    </tr>
    <tr>
        <td colspan="3">
            <asp:Button ID="btnPrintToken" runat="server" Text="Print Token" 
                onclick="btnPrintToken_Click" />
        </td>
    </tr>
    <tr>
        <td colspan="3">
            <asp:Label ID="lblCurrentStatus" runat="server" Font-Size="Medium">
            </asp:Label>
        </td>
    </tr>
</table>

WebForm1.aspx.cs 
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["TokenQueue"] == null)
        {
            Queue<int> tokenQueue = new Queue<int>();
            Session["TokenQueue"] = tokenQueue;
        }
    }

    protected void btnCounter1_Click(object sender, EventArgs e)
    {
        ServerNextCustomer(txtCounter1, 1);
    }

    protected void btnCounter2_Click(object sender, EventArgs e)
    {
        ServerNextCustomer(txtCounter2, 2);
    }

    protected void btnCounter3_Click(object sender, EventArgs e)
    {
        ServerNextCustomer(txtCounter3, 3);
    }

    protected void btnPrintToken_Click(object sender, EventArgs e)
    {
        Queue<int> tokenQueue = (Queue<int>)Session["TokenQueue"];
        lblCurrentStatus.Text = "There are " + tokenQueue.Count.ToString() 
            + " customers before you in the queue";

        if (Session["lastTokenNumberIssued"] == null)
        {
            Session["lastTokenNumberIssued"] = 0;
        }

        int nextTokenNumberToIssue = (int)Session["lastTokenNumberIssued"] + 1;
        Session["lastTokenNumberIssued"] = nextTokenNumberToIssue;
        tokenQueue.Enqueue(nextTokenNumberToIssue);
            
        AddTokenNumbersToListBox(tokenQueue);
    }

    private void AddTokenNumbersToListBox(Queue<int> tokenQueue)
    {
        listTokens.Items.Clear();
        foreach (int token in tokenQueue)
        {
            listTokens.Items.Add(token.ToString());
        }
    }

    private void ServerNextCustomer(TextBox textBox, int counterNumnber)
    {
        Queue<int> tokenQueue = (Queue<int>)Session["TokenQueue"];
        if (tokenQueue.Count > 0)
        {
            int tokenNumberToBeServed = tokenQueue.Dequeue();
            textBox.Text = tokenNumberToBeServed.ToString();
            txtNextToken.Text = "Token Number : " + tokenNumberToBeServed.ToString() 
                + ", please go to Counter " + counterNumnber.ToString();
            AddTokenNumbersToListBox(tokenQueue);
        }
        else
        {
            textBox.Text = "No cutomers in Queue";
        }
    }
}

5 comments:

  1. Hi Venkat,

    Thanks for the video. Very nice explanation. I want to add something. We can essentially get the same result if we use "oncommand" event for all the three "Next" buttons and can handle in a single method instead of 3 separate methods for the button click events.

    ReplyDelete
  2. Nice Explanation Sir! and I have qa uestion on how cum this session variable is incrementing with out we are setting? on each button clicked event (Queue)Session["TokenQueue"]

    ReplyDelete
  3. Sir Can You Give This Example on C# in Windows Form Thanks .....replay must i'll be wait

    ReplyDelete
  4. Nice Explanation Sir! and I have qa uestion on how cum this session variable is incrementing with out we are setting? on each button clicked event (Queue)Session["TokenQueue"]

    ReplyDelete
  5. Here only tokenQueue.Enqueue(nextTokenNumberToIssue) on printToken button
    not Session["TokenQueue"] = tokenQueue adding in printbutton click event then how Session["TokenQueue"] value assing tokenQueue value, please reply sir

    ReplyDelete

It would be great if you can help share these free resources