Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

I am working on excel plugin, when I need to show a spinner till some operation happens in the background. I took an approach where I have placed the spinner image(gif) in another windows form and call it in a separate thread. Once the operation gets complete, I abort this thread which is running the spinner form.

This is giving thread abort exception randomly.

What I have tried:

Here is my code:

Spinner form:

<pre>public partial class LoadingScreen : Form
    {
        private LoadingScreen _loadingScreen = null;
        private Thread oThread = null;

        public LoadingScreen()
        {
            InitializeComponent();
        }

        public LoadingScreen(bool showloading)
        {
            InitializeComponent();

            ShowLoadingScreen();
        }
        public void FocusLoading()
        {
            if (_loadingScreen == null)
                ShowLoadingScreen();
            if (oThread != null)
            {
                this.Activate();
            }
        }
        private void ShowFormOpacity()
        {
            _loadingScreen = new LoadingScreen();
            _loadingScreen.Opacity = .75;
            _loadingScreen.Controls["lblLoading"].Visible = false;
            if (_loadingScreen != null)
                Application.Run(_loadingScreen);
        }

        public void CloseFormInstance()
        {
            if (oThread != null)
            {
                try
                {
                    _loadingScreen = null;
                    oThread.Abort();
                    oThread.Join();
                }
                catch (Exception ex) { }
            }
        }
        public void ShowLoadingScreen()
        {
            if (_loadingScreen != null)
                return;
            oThread = new Thread(new ThreadStart(ShowFormOpacity));
            oThread.IsBackground = false;
            oThread.SetApartmentState(ApartmentState.STA);
            oThread.Start();
            while (_loadingScreen == null || _loadingScreen.IsHandleCreated == false)
            {
                System.Threading.Thread.Sleep(50);
            }
        }
    }



Calling the spinner form from ThisAddIn.cs

private LoadingScreen screen;


public void LoadingScreen(bool showloading)
{
    if (screen != null)
    {
        screen.FocusLoading();
    }
    else
    {
        screen = new LoadingScreen(showloading);
    }
}
public void CloseFormInstance()
{
    if (screen != null)
    {
        screen.CloseFormInstance();
    }
}



Why is the thread abort exception happens sometimes?
Posted
Updated 30-Sep-17 3:26am

1 solution

You're doing it backwards. The code you're running is on the UI (startup) thread. ALL UI STUFF, including your "spinner form" MUST be on the UI thread. Every little touch of every UI form and control MUST be done on the UI thread. Creating a thread to show a form or a control on it isn't going to work.

It's the WORK you have to put on a background thread.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900