相关疑难解决方法(0)

如何抑制thread.abort()错误C#?

我的程序加载时,我在后台线程上显示启动画面.加载后我将中止Thread,因为它的唯一目的是显示一个Now Loading飞溅表单.

我的问题是,当中止一个Thread时,它会抛出一个ThreadAbortException用户只需单击Continue on.

我该如何处理?我试图压制它 - >

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

        }
Run Code Online (Sandbox Code Playgroud)

但我有一种感觉,这会让我在这里大吼大叫,它无论如何都行不通.

谢谢!

.net c# multithreading error-suppression thread-abort

5
推荐指数
3
解决办法
9237
查看次数

在不同的线程上使用Application.Run()

请看下面的代码:

var splashForm = new SplashForm();
m_Thread = new Thread( () => System.Windows.Forms.Application.Run( splashForm ) )
m_Thread.Start();

// Do some initialization
// ...

// the following method just invokes `Close()` on the right thread
splashForm.Shutdown();

// Loop until the thread is no longer alive
// ...

System.Windows.Forms.Application.Run( mainForm );
Run Code Online (Sandbox Code Playgroud)

它看起来好像一切正​​常:首先我看到了闪屏,后来主变形开始了.但不知怎的,我得到了奇怪的错误,例如:图形元素(无尽的ProgressBar)没有正确显示.
编辑:我有两个进度条,一个在启动画面上,在主窗体上.他们都在无尽模式中表现出相同(错误)的行为:没有进步,只有纯粹的背景./编辑
在我看来,这是由于Application.Run()不同线程的调用.在启动启动画面之前,可以通过调用mainForm的任何函数/属性来消除此错误 - 例如

mainForm.Text = mainForm.Text;
Run Code Online (Sandbox Code Playgroud)

任何人都可以请确认此代码可能会导致问题 - 或者它应该表现良好,我必须在其他地方寻找错误?
我已经看过了flashscreen实现,我知道它可以以不同的方式完成.但我有兴趣了解这个实现及其可能存在的问题.谢谢!

c# multithreading

5
推荐指数
1
解决办法
2万
查看次数