简体   繁体   中英

Events Not Queueing

I am trying to handle multiple button click events in such a way that the events will be executed one after the other using Queue(). Somehow no events are being queued when i print the Count and i can't figure out why. The code is as follows.

        private void callBtn0_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if ((btn.Name == ("btn" + 0))
            {
                buttonQueue.Enqueue(btn);
                closeDoors();
            }
            else if ((btn.Name == ("btn" + 1))
            {
                buttonQueue.Enqueue(btn);
                closeDoors();
            }
            while(buttonQueue.Count > 0)
            {
                buttonQueue.Dequeue();
                listBox.Items.Add("Number: " + buttonQueue.Count);
            }

You are dequeuing just added item, thus next call to Count returns zero:

// add one item

while(buttonQueue.Count > 0) // Count is 1
{
    buttonQueue.Dequeue(); // remove one item
    listBox.Items.Add("Number: " + buttonQueue.Count); // Count is 0
}

A subsequent UI thread call to callBtn0_Click cannot execute until an existing call completes. Based on your code, only 1 item can be in the queue and you dequeue said item immediately after adding it.

I suggest you read about UI threads and the message pump

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM