简体   繁体   中英

Thread abort exception in WPF

I am trying to implement loaders in my WPF application. During some heavy operations, the UI thread gets frozen, so I had to implement the loaders using threads. Each time the loader loads, a new thread is created and this thread gets aborted (manually) when the loader sets off. The problem I am facing is that sometimes the application gets crashed giving a ThreadAbortException .

This is the code to start the loader :

try
        {
            //if(newWindowThread !=null && !newWindowThread.IsAlive) { }
            newWindowThread = new Thread(new ThreadStart(() =>
            {
                try
                {
                    // Create and show the Window
                    awq = new BusyIndicatorDisguise(BusyMessage);
                    awq.Show(); // <== POINT WHERE THE EXCEPTION IS THROWN
                    //Start the Dispatcher Processing
                    if (!isDispatcherStarted)
                    {
                        var a = Thread.CurrentThread;
                        var b = Dispatcher.CurrentDispatcher;
                        //isDispatcherStarted = true;
                        Dispatcher.Run();
                    }

                }
                catch (ThreadAbortException thEx)
                {

                }
                catch (Exception ex)
                {

                }
            }
            ));
            // Set the apartment state
            newWindowThread.SetApartmentState(ApartmentState.STA);
            // Make the thread a background thread
            newWindowThread.IsBackground = true;
            // Start the thread
            newWindowThread.Start();
        }
        catch (Exception ex)
        {

        }

This code is for stopping the loader :

if (newWindowThread != null && newWindowThread.IsAlive)
        {          
            newWindowThread.Abort();              
        }

I am not able to catch this exception in my catch block. Maybe because it is on a different thread. I want to know how can I avoid the ThreadAbortException

You should add a try catch block in the thread that may throw the exception and manage it accordingly to your needs.

Anyway, as @Josh says in other similar post

There are much better ways of aborting a thread without using Thread.Abort which not only chaotically interrupts your code at an unpredictable point, it's also not guaranteed to work because if your thread is currently calling out to some unmanaged code, the thread will not abort until control returns to managed code.

It's much better to use some type of synchronization primitive such as aManualResetEvent to act as a flag telling your thread when to exit. You could even use a boolean field for this purpose which is what the BackgroundWorker does.

If you throw an exception for yourself, forget the Thread.Abort . That's why:

  • Throwing an exception is an extremely costly operation. It saves the whole call stack and other useful data for debugging. In this case all you need is just to set a simple flag.
  • ThreadAbortException is a tricky one. It is automatically re-thrown at the end of the exception handler block unless you call Thread.ResetAbort in the catch block. But do NOT do that !
  • A ThreadAbortException is an asynchronous exception that means it can occur at any point of your code, which may cause unpredictable results. It is a brute force tool such as the End task button in the Task Manager. Use it only if you cannot rewrite the executed code (3rd party component) and if you are sure that you can unload the remains of the unstable execution environment (it is executed in an AppDomain ).

Instead, send a cancellation request to your loader (can be a simple bool ), which you should poll regularly during the load operation. Here is an example how you can do it by using a BackgroundWorker and a volatile field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM