S
S
SergeySerge112022-02-20 17:32:41
WPF
SergeySerge11, 2022-02-20 17:32:41

How to prevent multiple button clicks? WPF?

There is a button, when pressed, a long operation takes place, at the end of which the rendering of the image/ element is affected.
The element image.Source = new DrawingImage(dGroup); dGroup contains drawingVisual elements that are removed and added during the click function.
The problem is if I click 10 times then it will do the calculations 10 times. And I don’t understand how to stop them at all.
By debugging, all click events are queued and executed one after another, but Animation works, as I understand it, in another thread. How to disable processing while image is being redrawn?

This is the kind of thing that doesn't work.

private void solveClick(object sender, EventArgs e)
        {
             
                if (!((Button)sender).IsEnabled) // пробoвал  по таймеру,  не помoгает, все вызовы 
                    return;
                else
                {
                    ((Button)sender).IsEnabled = false;
                
                     var selectedTabItem =(TabItemInterface) (this.TabsFunc.SelectedItem);
                    if (selectedTabItem.IsReady())  
                    {
                        selectedTabItem.Calculate( ); // в конце вычислений обновляет содержимое DawingGroup
                       // this.UpdateLayout();
                        ((Button)sender).IsEnabled = true;
                    }
                } 
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2022-02-20
@d-stream

On click - disable the button [and possibly other related ui elements + unlock / show others] and then run something long in a separate thread .. upon its completion - unlock.
Classic: two buttons "start" and "stop" + progressbar
IsEnabled for buttons clings to the boolean field IsProcess (inversely for start and right for stop)
ProgressBar - visible only when IsProcess == true

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question