线程中止异常随机发生 [英] Thread abort exception occurs randomly

查看:57
本文介绍了线程中止异常随机发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用excel插件,当我需要显示一个微调器,直到某些操作在后台发生。我采用了一种方法,我将旋转器图像(gif)放在另一个窗体中,然后在一个单独的线程中调用它。一旦操作完成,我就会中止运行微调器形式的这个线程。



这是随机提供线程中止异常。



我尝试过:



这是我的代码:

Spinner表格:

< pre>公共部分类LoadingScreen:表格
{
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 )
返回;
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);
}
}
}







调用微调窗体来自ThisAddIn.cs



私人LoadingScreen屏幕; 





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







为什么线程中止有时会发生异常吗?

解决方案

你正在倒退。您正在运行的代码位于UI(启动)线程上。所有UI STUFF,包括你的微调形式必须在UI线程上。每个UI表单和控件的每一点触摸都必须在UI线程上完成。创建一个显示表单或控件的线程是不行的。



这是你必须把它放在后台线程上的工作。

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?

解决方案

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.


这篇关于线程中止异常随机发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆