views:

521

answers:

3

I have a Form being launched from another form on a different thread. Most of the time it works perfectly, but I get the below error from time to time. Can anyone help?

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()

A: 

Can you elaborate what you are trying to do here? If you are trying to show a Form from a different thread than the UI thread then refer to this question: http://stackoverflow.com/questions/160555/my-form-doesnt-properly-display-when-it-is-launched-from-another-thread

Vivek
A: 

The application is an Explorer-Type customer management system. An account form is launched from the "Main" explorer form on a separate background thread. We do this because the user needs to be able to have multiple accounts open at the same time.

We launch the form using this code:

Thread = New Thread(AddressOf ShowForm)
Thread.SetApartmentState(ApartmentState.STA)
Thread.IsBackground = True
joek1975
+1  A: 

The user has to be able to see multiple open accounts simultaneously, right? So you need multiple instances of a form?

Unless I'm misreading something, I don't think you need threads for this scenario, and I think you are just introducing yourself to a world of hurt (like these exceptions) as a result.

Assuming your account form is called AccountForm, I'd do this instead:

Dim acctForm As New AccountForm()
acctForm.Show()

(Of course you'll have your own logic for that ... ) I might even put it in the ShowForm method so that I could just update my caller thusly:

ShowForm()

And be done. Now all of this assumes that you've encapsulated the AccountForm nicely so that each instance has its own data, and they don't share anything between instances.

Using threads for this is not only overkill, but likely to introduce bugs like the exception at the top. And my experience in debugging multi-threaded WinForms apps has shown that these bugs are often very difficult to replicate, and extremely tricky to find and fix. Oftentimes, the best fix is to not multithread unless you absolutely, positively have to.

John Rudy
I tend to agree with you about multithreading. In the case of this application, when I asked why each form is launched in a separate thread, the reason is because since the forms are so large and the data displayed is so large, it was necessary in order to improve performance.
joek1975
In any case, I discovered that the form was being created on the main thread, and then shown on a new thread, which would cause some cross-threading issues.
joek1975