How do I prevent any button click events from queuing until the event handle is finished with the first call

后端 未结 2 1131
时光取名叫无心
时光取名叫无心 2021-01-12 15:20

I want to prevent a button click from queuing. In testing I have a Form, a Button and in the Code-Behind I have the event handler:

    private void button1_C         


        
2条回答
  •  感情败类
    2021-01-12 15:42

    I had a button that on click event was going to run a method. Same issue happent and when the user clicked multiple times the method was triggered multiple times. So I made a boolean and changed it value when the method started.

    private bool IsTaskRunning = false

    private void MyMethod(){
    
    if(IsTaskRunning==false){
    
           IsTaskRunning=true;
    
           //My heavy duty code that takes a long time
    
            IsTaskRunning=false;    //When method is finished
    
       }
     }
    

    So now the method runs only if it's done the last time.

提交回复
热议问题