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)));
}
}
}