views:

193

answers:

1

Hi all,

I have a button which has an animation (in xaml) on it's click event. Cool so far.

Problem is that I also have processing occurring on the click event (so I can do stuff) - and this occurs first.

How do I prioritise or re-order so that the animation takes place before any custom processing...

Thanks.

A: 

The problem is that the UI thread is blocked while your processing is running. You can move the processing in a background thread. Example (C#):

public Window1() {
    private bool processingRunning = false;

    private void button1_Click(object sender, RoutedEventArgs e) {
        if (!processingRunning) {  // avoid starting twice
            var bw = new BackgroundWorker();

            bw.DoWork += (s, args) => {
                // do your processing here -- this will happen in a separate thread
                ...
            };

            bw.RunWorkerCompleted += (s, args) => {
                processingRunning = false;
                if (args.Error != null)  // if an exception occurred during DoWork,
                    MessageBox.Show(args.Error.ToString());  // do your error handling here
            };

            bw.RunWorkerAsync(); // starts the background worker
        }
    }
}
Heinzi
Whilst this looks like the answer in general it won't work in one of my specific instances. I am catching the "closing" event of the window and throwing up a "Are you sure you want to exit" messagebox - however if I add that into the background worker the program closes as it finishes processing the stuff. Any chance I can put the animation in the background thread?
Matt B