我如何晚饭preSS一个Thread.Abort的()错误C#?晚饭、错误、Thread、preSS

2023-09-03 00:16:48 作者:一腔孤勇又如何

我显示在后台线程,而我的程序加载启动画面。一旦它加载我终止线程,因为它的唯一目的是要表明一个现在载入飞溅的形式。

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.