Nullqwerty
Nullqwerty

Reputation: 1168

How To Detect When UI Thread Starts and Stops Processing in WPF with C#?

In my WPF C# application, I have a little circle animation that I display while things are processing to let the user know that the application is "thinking" and they should just chill out. It's a separate exe. So, for example, when the user clicks a button to run analytics (which might take 5-10 seconds), I start the process to display the animation, and when it's all complete, I stop the process. During this time, sub-threads have been newly spawned to handle background tasks. When completed the UI is updated, which can take a few seconds due to complex datagrids.

While this technically works, I'd rather approach it in a different manner. I'd rather have the application auto-detect when the threads are working and automatically display the animation, and when the threads all complete, it stops displaying it. I can detect the background threads easily, but how can I detect the UI thread? I'd rather do it this way because there have been a couple of times that I accidentally missed killing the application and it was still running after processing completed. This would prevent that.

Are there events that fire when the UI thread starts and stops? If not, let's say I threw a timer in there that's fired every 1/2 second, is there a call I could make that would tell me the state of the UI thread?

Thanks

Upvotes: 2

Views: 1418

Answers (3)

Sphinxxx
Sphinxxx

Reputation: 13017

You could try to add a task to the main UI thread with a really low priority, through the application's Dispatcher:

Application.Current.Dispatcher.BeginInvoke((ThreadStart)StopAnimation, DispatcherPriority.ApplicationIdle);

...

private void StopAnimation()
{
    ...
}

This basically says "When the main UI thread has absolutely nothing better to do, call StopAnimation()". The trick is to find the right time to call .BeginInvoke - just before or during the grid loading.

Upvotes: 1

oleksii
oleksii

Reputation: 35895

A better approach would be to move processing out of UI (for example with BackgroundWorker and let the background worker post notifications to the UI thread (started, progress update, finished).

Do not reinvent a wheel and move processing logic out of the UI thread.

Upvotes: 2

Servy
Servy

Reputation: 203802

It's a catch-22. If you had some code that you could run to determine that the UI thread was blocked, then you wouldn't be able to start an animation because the UI thread is blocked; the animation wouldn't be shown until after the UI thread finished the tasks it had been working on, at which point the animation is no longer relevant.

In virtually all cases you should be moving your long running tasks to another background thread, not doing them in the UI thread. You can explicitly disable some/all of the controls on your form at the start of such tasks and enable them at the end. I would highly discourage you from blocking the UI thread as a means of disabling the form as, from a user perspective, it will appear as if the program isn't working due to a programming bug, as opposed to thinking it's desired behavior.

Upvotes: 1

Related Questions