c# button click queueing

断了今生、忘了曾经 提交于 2019-12-11 13:25:28

问题


I have a button click event handler with a switch case in it that controls multiple buttons in one event handler.

I need to use a queue because while one button is clicked and doing some processing, second button click won't interfere with the first button click, but added to the queue. I don't want to use .enabled=false; because it'll discard the second click completely, and I'm currently editing someone's software at work so I don't want to break things that I don't know, so what are you suggesting?


回答1:


The best idea, I think, is to create a producer/consumer queue.

Another question is explaining this technique.

Basically, the idea is to have a worker thread that will consume a queue to get the job to do, while other thread produce job by queuing operation in the queue.




回答2:


I did succeed this with System.Collections.Queue

The code is :

private Queue<Button> Button_Queue = new Queue<Button>();
private bool isProcessing = false;

private void Button_Click((object sender, EventArgs e){

if(isProcessing){
    Button_Queue.Enqueue(this);
}
else
{
    isProcessing = true;

    // code here

    isProcessing = false;
    while(Button_Queue.Count > 0){
        Button_Queue.Dequeue().PerformClick();
    }
}

of course mine is slightly different from this because I need to pass some variables and my click method is modified for this.



来源:https://stackoverflow.com/questions/11096352/c-sharp-button-click-queueing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!