0
Below I have a code that he needs to do the following: Create a thread, then start it. When starting the thread, according to the time (random time) the program adds a value in the queue, after being added in the queue, it needs to show how many elements there are in the queue. However, when executed, the program does not exceed 1 element in the queue (even though it has already passed the time to add another element). How to solve this problem?
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)));
}
}
}
The goal is to simulate the queue of a store, but with your answer I managed to find a solution
– Braian Freitas