How to add elements in Queue continuously?

0

Below I have a code that it needs to do the following: Create a thread, then start it. When you start the thread, according to the time (random time) the program adds a value in the queue, after being added to the queue, it needs to show how many elements it has in the queue. However, when executed, the program is only 1 element in the queue (even though the time for adding another element has already passed). How do I fix this?

namespace MultiThreads
{
    public partial class Form1 : Form
    {
        private Queue<int> FilaClientes = new Queue<int>();
        List<Thread> lt = new List<Thread>();

        public Form1()
        {
            InitializeComponent();
        }

        private static void UpdateFila(TextBox tx, Queue<int> Fila)
        {
            tx.Text = Fila.Count.ToString();
        }

        private void AdicionaClientesFila(TextBox tx)
        {
            Stack<int> TempoParaFila = new Stack<int>();
            Random TempoVaria = new Random((int)DateTime.Now.Ticks & 
                                            0x0000FFFF);
            TempoParaFila.Push(TempoVaria.Next(1, 1001));
            int Aux = TempoParaFila.Pop();
            Thread.Sleep(Aux);

            //acima é descrito o valor em tempo para 
            //chegar alguém na fila

            Random ItensFila = new Random((int)DateTime.Now.Ticks & 
                                            0x0000FFFF);
            Stack<int> NovoCliente = new Stack<int>();
            NovoCliente.Push(ItensFila.Next(1, 1001));
            FilaClientes.Enqueue(NovoCliente.Pop());
            //Acima é adicionado a quantidade de itens do cliente

            if (txtboxTamanhoFila.InvokeRequired)
            {
                MethodInvoker m = new MethodInvoker(() => UpdateFila(tx, 
                                                     FilaClientes));
                tx.Invoke(m);
            }
            else
            {
                UpdateFila(tx, FilaClientes);
            }

            Application.DoEvents();
        }

        private void btnInicia_Click(object sender, EventArgs e)
        {
            foreach(Thread t in lt)
            {
                t.Start();
                Thread.Sleep(500);
            }
        }

        private void btnCriar_Click(object sender, EventArgs e)
        {
            lt.Add(new Thread(() => 
                        AdicionaClientesFila(txtboxTamanhoFila)));
        }
    }
}
    
asked by anonymous 01.05.2018 / 02:13

1 answer

2

Quick solution Use a static concurrent queue.

private static ConcurrentQueue<int> FilaClientes = new Queue<int>();

Considerations

  • Avoid using new Thread (). When creating a new thread, it will be created in a processor core and the load will not be distributed. That is, depending on the processing processes in this core will be affected and overall performance will drop. Whenever possible, use Tasks (TPL link ). The .net people solved this a lot good.
  • Instead of always firing a new thread (or task) in the click, you can in the form creation start a task that will be inside a looping while looking at the queue. When you have an item in the queue, it processes.
  • I did not understand the use of the stack in the AddClientFile method. The instruction is Sleep (RandomTime), Enqueue (RandomValue).

        var time = new Random().Next(minValue: 200, maxValue: 5000);
        Task.Delay(millisecondsDelay: time);
    
        var item = new Random().Next(minValue: 100, maxValue: 10000);
        FilaClientes.Enqueue(item: item);
    
  • I suggest you read the style guid from .Net (local variables start with lowercase letters, use var for local variables). link

  • If you are in doubt, please elaborate further on your goal.

        
    02.05.2018 / 14:05