Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I suppress a thread.abort() error C#?

I am showing a splash screen on a background thread while my program loads. Once it loads I am aborting the Thread as it's only purpose was to show a Now Loading splash form.

My problem is that when aborting a Thread it throws a ThreadAbortException that the user can just click Continue on.

How do I deal with this? I was trying to suppress it like so -->

            try
        {
            Program.splashThread.Abort();
        }
        catch(Exception ex)
        {

        }

but I have a feeling that is going to get me yelled at here and it doesn't work any way.

Thanks!

like image 297
Refracted Paladin Avatar asked Jun 04 '09 14:06

Refracted Paladin


People also ask

What will happen if the Abort method is called on a thread?

If the thread that calls Abort holds a lock that the aborted thread requires, a deadlock can occur. If Abort is called on a thread that has not been started, the thread will abort when Start is called. If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted.

How do I stop my AC threading?

By using thr. Abort(); statement, we can terminate the execution of the thread.

What does thread was being aborted mean?

If an application is idle for some time (meaning that no requests are coming in), or certain other conditions are met, ASP.NET will recycle the entire AppDomain . When that happens, any threads that you started from that AppDomain , including those from Application_Start , will be aborted.

What is thread Abort exception C#?

When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException . ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.


2 Answers

You don't need to cancel the thread. I'll exemplify with code.

In the splash screen form:

public void CloseSplash()
{
    Invoke((MethodInvoker)delegate
    {
        this.Close();
    });
}

In the Program.cs file:

private static Splash _splash = null;
public static void CloseSplash()
{
    if (_splash!= null)
    {
        _splash.CloseSplash();
    }
}

Now, when your Main method starts, show the splash in a thread:

Thread t = new Thread(new ThreadStart(delegate
{
    _splash = new Splash();
    _splash.ShowDialog();
}));

t.Start();

...and when you want it to close, just close it:

Program.CloseSplash();

Then you don't need to worry about aborting the thread; it will exit gracefully.

like image 106
Fredrik Mörk Avatar answered Sep 23 '22 10:09

Fredrik Mörk


Please refer to the following link obtained doing a Google search (first result returned):

http://msdn.microsoft.com/en-us/library/5b50fdsz.aspx

Pay special attention to this part:

When this method is invoked on a thread, the system throws a ThreadAbortException in the thread to abort it. ThreadAbortException is a special exception that can be caught by application code, but is re-thrown at the end of the catch block unless ResetAbort is called. ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread. Unexecuted finally blocks are executed before the thread is aborted.

like image 24
CodeWarrior Avatar answered Sep 23 '22 10:09

CodeWarrior