通用GDI +错误
时间:2020-03-06 15:02:29 来源:igfitidea点击:
我在另一个线程上从另一个表单启动了一个Form。在大多数情况下,它都能正常运行,但有时会出现以下错误。有人可以帮忙吗?
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format) at System.Drawing.Bitmap..ctor(Int32 width, Int32 height) at System.Drawing.Icon.ToBitmap() at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t) at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t) at System.Windows.Forms.Control.WndProcException(Exception e) at System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Form.ShowDialog(IWin32Window owner) at System.Windows.Forms.Form.ShowDialog()
解决方案
我们能在这里详细说明我们要做什么吗?
如果我们尝试从不同于UI线程的线程中显示表单,请参考以下问题:
从另一个线程启动时,我的表单无法正确显示
该应用程序是一个Explorer类型的客户管理系统。在单独的后台线程上从"主"浏览器表单启动一个帐户表单。我们这样做是因为用户需要能够同时打开多个帐户。
我们使用以下代码启动表单:
Thread = New Thread(AddressOf ShowForm) Thread.SetApartmentState(ApartmentState.STA) Thread.IsBackground = True
用户必须能够同时看到多个打开的帐户,对吗?因此,我们需要一个表单的多个实例?
除非我误读了一些东西,否则我认为我们不需要这种情况的线程,而且我认为我们只是在向自己介绍一个遭受痛苦的世界(如这些异常)。
假设帐户表单称为AccountForm,我可以这样做:
Dim acctForm As New AccountForm() acctForm.Show()
(当然,我们将有自己的逻辑……)我什至可以将其放在" ShowForm"方法中,这样就可以由此更新我的调用者:
ShowForm()
并完成。现在,所有这些都假设我们已经很好地封装了AccountForm,以便每个实例都有自己的数据,并且它们在实例之间不共享任何数据。
为此使用线程不仅过分,而且可能会引入诸如顶部异常之类的错误。我在调试多线程WinForms应用程序中的经验表明,这些错误通常很难复制,并且很难发现和修复。通常,最好的解决方法是不要多线程,除非我们绝对肯定地必须这样做。