This is a migrated thread and some comments may be shown as answers.

How to clear routed events from EventManager ?

0 Answers 151 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joël
Top achievements
Rank 2
Joël asked on 28 Sep 2012, 07:45 AM
Hi there,

I am currently having troubles with RoutedEvent in WPF. I made a simple example only using native WPF components. The example is made of a button executing a long term synchronous task the first time it's hit and showing a message "Already executed" all other times.

What you have to do to reproduce my issue is to click the button once, and when the task is executing click a second time => After finishing the first task the second click event is going to trigger a second execution => "Already executed". I want to consume any user input during the execution before it triggers an event.

How could I do that ?

<Window x:Class="TestDBConnection.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
         
        <Button Name="butt" Height="30" Width="150" />
        <Label Visibility="Collapsed" Name="label">Executing ... </Label>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Objects;
 
namespace TestDBConnection
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
 
            var cmd = new MyCommand();
            //butt.Command = cmd;
 
            //butt.PreviewMouseDown += new MouseButtonEventHandler(butt_PreviewMouseDown);
            butt.Click += butt_Click;
 
            cmd.label = label;
            cmd.bouton = butt;
 
        }
 
        void butt_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            //label.Visibility = Visibility.Visible;
        }
 
        void butt_Click(object sender, RoutedEventArgs e)
        {
            if (executedAlready)
            {
                MessageBox.Show("Already Executed");
                return;
            }
 
            for (long i = 0; i < 40000; i++)
            {
                for (long j = 0; j < 40000; j++)
                {
                    var a = i + j;
                }
            }
 
            //label.Visibility = Visibility.Collapsed;
            executedAlready = true;
        }
 
        private bool executedAlready;
    }
 
    public class MyCommand : ICommand
    {
 
        public bool CanExecute(object parameter)
        {
            return true;
        }
 
        public event EventHandler CanExecuteChanged;
 
        public void Execute(object parameter)
        {
            if (executedAlready)
            {
                MessageBox.Show("Already Executed");
                return;
            }
 
            for (long i = 0; i < 40000; i++)
            {
                for (long j = 0; j < 40000; j++)
                {
                    var a = i + j;
                }
            }
 
            executedAlready = true;
 
            label.Visibility = Visibility.Collapsed;
        }
 
        public Label label;
        public Button bouton;
        public Window window;
 
        private bool executedAlready;
    }
}

No answers yet. Maybe you can help?

Tags
General Discussions
Asked by
Joël
Top achievements
Rank 2
Share this question
or