我如何晚饭preSS一个Thread.Abort的()错误C#? [英] How do I suppress a thread.abort() error C#?

查看:165
本文介绍了我如何晚饭preSS一个Thread.Abort的()错误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.

我的问题是,放弃它抛出一个线程时, ThreadAbortException ,用户只需单击继续上。

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

我该如何面对呢?我试图晚饭preSS它像这样 - >

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.

谢谢!

推荐答案

您不必取消线程。我将举例说明了code。

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

在启动画面形式:

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

在Program.cs文件:

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.

这篇关于我如何晚饭preSS一个Thread.Abort的()错误C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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