WPF exe is very slow compared to working in Visual Studio

I created an application in WPF using the MVVM template.

The application works fine in the Visual Studio debugger, but when I run exe from the debug / release folder, it becomes very slow.

Here is my class RelayCommand:

public class RelayCommand : ICommand
{
    private readonly Action<object> execute;
    private readonly Predicate<object> canExecute;

    public RelayCommand(Action<object> execute) : this(execute, DefaultCanExecute)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        this.execute = execute;
        this.canExecute = canExecute;
    }

    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }

        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        bool res = false;
        if (canExecute != null)
        {
            res = canExecute(parameter);
        }

        return res;
    }

    public void Execute(object parameter)
    {
        execute(parameter);
    }

    private static bool DefaultCanExecute(object parameter)
    {
        return true;
    }
}

If I remove the method from my class , the exe version will work as usual. CanExcecute()RelayCommand

Please explain why this is happening. Is this for an event handler CanExecuteChanged?

+4
source share
1 answer

You have two options:

  • Do not use the event CommandManager.RequerySuggested.

CommandManager Dispatcher DispatcherPriority.Background. , , , CommandManager CanExecute() , CommandManager. - (, ), , , .

. Prism ICommand CommandManager. , RaiseCanExecuteChanged(), . , , .

  1. CanExecute().

, :

public bool CanExecute()
{
    return this.isIdle && this.someFlag && !this.CommandAbort.CanExecute();
}
+1

Source: https://habr.com/ru/post/1662412/


All Articles